home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / biblio / bibtex / distribs / bibtex.web (.txt) < prev   
LaTeX Document  |  1993-11-24  |  383KB  |  9,200 lines

  1. % This program is copyright (C) 1985 by Oren Patashnik; all rights reserved.
  2. % Copying of this file is authorized only if (1) you are Oren Patashnik, or if
  3. % (2) you make absolutely no changes to your copy. (The WEB system provides
  4. % for alterations via an auxiliary file; the master file should stay intact.)
  5. % See Appendix H of the WEB manual for hints on how to install this program.
  6. % Version 0.98f was released in March 1985.
  7. % Version 0.98g was released in April; it removed some system dependencies
  8. %       (introducing term_in and term_out in place of just tty, and removing
  9. %       some nonlocal goto's) and it gave context for certain parsing errors.
  10. % Version 0.98h was released in April; it patched a bug in the output
  11. %       line-breaking routine that can arise with some nonstandard style files.
  12. % Version 0.98i was released in May; its main change split up the main program
  13. %       and some procedures to help certain compilers cope with size
  14. %       limitations, among other things changing error and warning macros so
  15. %       they'd produce (much) less inline code; it also redefined the class of
  16. %       legal style-file identifiers---although this affects only the bizarre
  17. %       ones, it makes BibTeX's error messages more coherent; and it had many
  18. %       minor changes, including about a 15% speed-up on TOPS-20.
  19. % Version 0.99a was released in January 1988.  Its main changes: allowed the
  20. %       inclusion of entire .bib files (rather than just those entries
  21. %       \cited or \nocited); made the sorting algorithm stable; eliminated
  22. %       any case conversion for file names; allowed concatenation in database
  23. %       fields and string definitions; handled hyphenated names properly;
  24. %       handled accented characters properly; implemented new empty$,
  25. %       preamble$, text.length$, text.prefix$, and warning$ built-in functions;
  26. %       allowed a new cross-referencing feature; and made many minor fixes,
  27. %       including about a 40% speed-up on TOPS-20.
  28. % Version 0.99b was released in February 1988.  It changed text.length$ and
  29. %       text.prefix$ to not count braces as text characters, and it changed
  30. %       text.prefix$ to add any necessary matching right braces.
  31. % Version 0.99c was released in February 1988.  It removed two begin-end pairs
  32. %       that, for convention only, surrounded entire modules, but that elicited
  33. %       label-related complaints from some compilers.
  34. % Please report any bugs to Oren Patashnik (PATASHNIK@@SCORE.STANFORD.EDU)
  35. % Although considerable effort has been expended to make the BibTeX program
  36. % correct and reliable, no warranty is implied; the author disclaims any
  37. % obligation or liability for damages, including but not limited to
  38. % special, indirect, or consequential damages arising out of or in
  39. % connection with the use or performance of this software.
  40. % This program was written by Oren Patashnik, in consultation with Leslie
  41. % Lamport, to be used with Lamport's LaTeX document preparation system.
  42. % Some modules were taken from Knuth's TeX and TeXware with his permission.
  43. % Here is TeX material that gets inserted after \input webmac
  44. \def\hang{\hangindent 3em\indent\ignorespaces}
  45. \font\ninerm=cmr9
  46. \let\mc=\ninerm % medium caps for names like PASCAL
  47. \def\PASCAL{{\mc PASCAL}}
  48. \def\ph{{\mc PASCAL-H}}
  49. \def\<#1>{$\langle#1\rangle$}
  50. \def\section{\mathhexbox278}
  51. \def\(#1){} % this is used to make section names sort themselves better
  52. \def\9#1{} % this is used for sort keys in the index via @@:sort key}{entry@@>
  53. % Note: WEAVE will typeset an upper-case `E' in a PASCAL identifier a
  54. % bit strangely so that the `TeX' in the name of this program is typeset
  55. % correctly; if this becomes a problem remove these three lines to get
  56. % normal upper-case `E's in PASCAL identifiers
  57. \def\drop{\kern-.1667em\lower.5ex\hbox{E}\kern-.125em} % middle of TeX
  58. \catcode`E=13 \uppercase{\def E{e}}
  59. \def\\#1{\hbox{\let E=\drop\it#1\/\kern.05em}} % italic type for identifiers
  60. \font\sc=cmcsc10
  61. \def\BibTeX{{\rm B\kern-.05em{\sc i\kern-.025em b}\kern-.08em
  62.     T\kern-.1667em\lower.7ex\hbox{E}\kern-.125emX}}
  63. \def\LaTeX{{\rm L\kern-.36em\raise.3ex\hbox{\sc a}\kern-.15em
  64.     T\kern-.1667em\lower.7ex\hbox{E}\kern-.125emX}}
  65. \def\title{\BibTeX\ }
  66. \def\today{\ifcase\month\or
  67.   January\or February\or March\or April\or May\or June\or
  68.   July\or August\or September\or October\or November\or December\fi
  69.   \space\number\day, \number\year}
  70. \def\topofcontents{\null\vfill
  71.  \def\titlepage{F}
  72.  \centerline{\:\titlefont The {\:\ttitlefont \BibTeX} preprocessor}
  73.  \vskip 15pt \centerline{(Version 0.99c---\today)} \vfill}
  74. \pageno=\contentspagenumber \advance\pageno by 1
  75. @* Introduction.
  76. @^documentation@>
  77. @^space savings@>
  78. @^system dependencies@>
  79. @^wizard@>
  80. @!@:BibTeX}{\BibTeX@>
  81. @!@:BibTeX documentation}{\BibTeX\ documentation@>
  82. @:LaTeX}{\LaTeX@>
  83. \BibTeX\ is a preprocessor (with elements of postprocessing as
  84. explained below) for the \LaTeX\ document-preparation system.  It
  85. handles most of the formatting decisions required to produce a
  86. reference list, outputting a \.{.bbl} file that a user can edit to add
  87. any finishing touches \BibTeX\ isn't designed to handle (in practice,
  88. such editing almost never is needed); with this file \LaTeX\ actually
  89. produces the reference list.
  90. Here's how \BibTeX\ works.  It takes as input (a)~an \.{.aux} file
  91. produced by \LaTeX\ on an earlier run; (b)~a \.{.bst} file (the style
  92. file), which specifies the general reference-list style and specifies
  93. how to format individual entries, and which is written by a style
  94. designer (called a wizard throughout this program) in a
  95. special-purpose language described in the \BibTeX\ documentation---see
  96. the file {\.{btxdoc.tex}}; and (c)~\.{.bib} file(s) constituting a
  97. database of all reference-list entries the user might ever hope to
  98. use.  \BibTeX\ chooses from the \.{.bib} file(s) only those entries
  99. specified by the \.{.aux} file (that is, those given by \LaTeX's
  100. \.{\\cite} or \.{\\nocite} commands), and creates as output a \.{.bbl}
  101. file containing these entries together with the formatting commands
  102. specified by the \.{.bst} file (\BibTeX\ also creates a \.{.blg} log
  103. file, which includes any error or warning messages, but this file
  104. isn't used by any program).  \LaTeX\ will use the \.{.bbl} file,
  105. perhaps edited by the user, to produce the reference list.
  106. Many modules of \BibTeX\ were taken from Knuth's \TeX\ and \TeX ware,
  107. with his permission.  All known system-dependent modules are marked in
  108. the index entry ``system dependencies''; Dave Fuchs helped exorcise
  109. unwanted ones.  In addition, a few modules that can be changed to make
  110. \BibTeX\ smaller are marked in the index entry ``space savings''.
  111. Megathanks to Howard Trickey, for whose suggestions future users and
  112. style writers would be eternally grateful, if only they knew.
  113. The |banner| string defined here should be changed whenever \BibTeX\
  114. gets modified.
  115. @d banner=='This is BibTeX, Version 0.99c' {printed when the program starts}
  116. @^system dependencies@>
  117. Terminal output goes to the file |term_out|, while terminal input
  118. comes from |term_in|.  On our system, these (system-dependent) files
  119. are already opened at the beginning of the program, and have the same
  120. real name.
  121. @d term_out == tty
  122. @d term_in == tty
  123. @^system dependencies@>
  124. This program uses the term |print| instead of |write| when writing on
  125. both the |log_file| and (system-dependent) |term_out| file, and it
  126. uses |trace_pr| when in |trace| mode, for which it writes on just the
  127. |log_file|.  If you want to change where either set of macros writes
  128. to, you should also change the other macros in this program for that
  129. set; each such macro begins with |print_| or |trace_pr_|.
  130. @d print(#) == begin write(log_file,#); write(term_out,#); end
  131. @d print_ln(#) == begin write_ln(log_file,#); write_ln(term_out,#); end
  132. @d print_newline == print_a_newline
  133.                                 {making this a procedure saves a little space}
  134. @d trace_pr(#) == begin write(log_file,#); end
  135. @d trace_pr_ln(#) == begin write_ln(log_file,#); end
  136. @d trace_pr_newline == begin write_ln(log_file); end
  137. @<Procedures and functions for all file I/O, error messages, and such@>=
  138. procedure print_a_newline;
  139. begin
  140. write_ln(log_file);
  141. write_ln(term_out);
  142. @^debugging@>
  143. @^statistics@>
  144. Some of the code below is intended to be used only when diagnosing the
  145. strange behavior that sometimes occurs when \BibTeX\ is being
  146. installed or when system wizards are fooling around with \BibTeX\
  147. without quite knowing what they are doing. Such code will not normally
  148. be compiled; it is delimited by the codewords
  149. `$|debug|\ldots|gubed|$', with apologies to people who wish to
  150. preserve the purity of English. Similarly, there is some conditional
  151. code delimited by `$|stat|\ldots|tats|$' that is intended only for use
  152. when statistics are to be kept about \BibTeX's memory/cpu usage,
  153. and there is conditional code delimited by `$|trace|\ldots|ecart|$'
  154. that is intended to be a trace facility for use mainly when debugging
  155. \.{.bst} files.
  156. @d debug == @{          { remove the `|@{|' when debugging }
  157. @d gubed == @t@>@}      { remove the `|@}|' when debugging }
  158. @f debug == begin
  159. @f gubed == end
  160. @d stat == @{           { remove the `|@{|' when keeping statistics }
  161. @d tats == @t@>@}       { remove the `|@}|' when keeping statistics }
  162. @f stat == begin
  163. @f tats == end
  164. @d trace == @{          { remove the `|@{|' when in |trace| mode }
  165. @d ecart == @t@>@}      { remove the `|@}|' when in |trace| mode }
  166. @f trace == begin
  167. @f ecart == end
  168. @^system dependencies@>
  169. We assume that |case| statements may include a
  170. default case that applies if no matching label is found,
  171. since most \PASCAL\ compilers have plugged this hole in the language by
  172. incorporating some sort of default mechanism. For example, the \ph\
  173. compiler allows `|others|:' as a default label, and other \PASCAL s allow
  174. syntaxes like `\ignorespaces|else|\unskip' or `\\{otherwise}' or
  175. `\\{otherwise}:', etc. The definitions of |othercases| and |endcases|
  176. should be changed to agree with local conventions.   Note that no semicolon
  177. appears before |endcases| in this program, so the definition of |endcases|
  178. should include a semicolon if the compiler wants one.  (Of course, if no
  179. default mechanism is available, the |case| statements of \BibTeX\ will have
  180. to be laboriously extended by listing all remaining cases. People who are
  181. stuck with such \PASCAL s have in fact done this, successfully but not
  182. happily!)
  183. @d othercases == others:        {default for cases not listed explicitly}
  184. @d endcases == @+end {follows the default case in an extended |case| statement}
  185. @f othercases == else
  186. @f endcases == end
  187. Labels are given symbolic names by the following definitions, so that
  188. occasional |goto| statements will be meaningful.  We insert the label
  189. `|exit|:' just before the `\ignorespaces|end|\unskip' of a procedure
  190. in which we have used the `|return|' statement defined below (and this
  191. is the only place `|exit|:' appears).  This label is sometimes used
  192. for exiting loops that are set up with the |loop| construction defined
  193. below.  Another generic label is `|loop_exit|:'; it appears
  194. immediately after a loop.
  195. Incidentally, this program never declares a label that isn't actually used,
  196. because some fussy \PASCAL\ compilers will complain about redundant labels.
  197. @d exit=10              {go here to leave a procedure}
  198. @d loop_exit=15         {go here to leave a loop within a procedure}
  199. @d loop1_exit=16        {the first generic label for a procedure with two}
  200. @d loop2_exit=17        {the second}
  201. @^for loops@>
  202. And |while| we're discussing loops: This program makes into |while|
  203. loops many that would otherwise be |for| loops because of Standard
  204. \PASCAL\ limitations (it's a bit complicated---standard \PASCAL\
  205. doesn't allow a global variable as the index of a |for| loop inside a
  206. procedure; furthermore, many compilers have fairly severe limitations
  207. on the size of a block, including the main block of the program; so
  208. most of the code in this program occurs inside procedures, and since
  209. for other reasons this program must use primarily global variables, it
  210. doesn't use many |for| loops).
  211. @^program conventions@>
  212. This program uses this convention: If there are several quantities in
  213. a boolean expression, they are ordered by expected frequency (except
  214. perhaps when an error message results) so that execution will be
  215. fastest; this is more an attempt to understand the program than to
  216. make it faster.
  217. Here are some macros for common programming idioms.
  218. @d incr(#) == #:=#+1    {increase a variable by unity}
  219. @d decr(#) == #:=#-1    {decrease a variable by unity}
  220. @d loop == @+ while true do@+   {repeat over and over until a |goto| happens}
  221. @f loop == xclause
  222.   {\.{WEB}'s |xclause| acts like `\ignorespaces|while true do|\unskip'}
  223. @d do_nothing ==        {empty statement}
  224. @d return == goto exit  {terminate a procedure call}
  225. @f return == nil
  226. @d empty=0              {symbolic name for a null constant}
  227. @d any_value=0          {this appeases \PASCAL's boolean-evaluation scheme}
  228. @* The main program.
  229. @^system dependencies@>
  230. @:LaTeX}{\LaTeX@>
  231. This program first reads the \.{.aux} file that \LaTeX\ produces,
  232. (\romannumeral1) determining which \.{.bib} file(s) and \.{.bst} file
  233. to read and (\romannumeral2) constructing a list of cite keys in order
  234. of occurrence.  The \.{.aux} file may have other \.{.aux} files nested
  235. within.  Second, it reads and executes the \.{.bst} file,
  236. (\romannumeral1) determining how and in which order to process the
  237. database entries in the \.{.bib} file(s) corresponding to those cite
  238. keys in the list (or in some cases, to all the entries in the \.{.bib}
  239. file(s)), (\romannumeral2) determining what text to be output for each
  240. entry and determining any additional text to be output, and
  241. (\romannumeral3) actually outputting this text to the \.{.bbl} file.
  242. In addition, the program sends error messages and other remarks to the
  243. |log_file| and terminal.
  244. @d close_up_shop=9998           {jump here after fatal errors}
  245. @d exit_program=9999            {jump here if we couldn't even get started}
  246. @t\4@>@<Compiler directives@>@/
  247. program BibTEX;                 {all files are opened dynamically}
  248. label   close_up_shop,@!exit_program @<Labels in the outer block@>;
  249. const   @<Constants in the outer block@>
  250. type    @<Types in the outer block@>
  251. var     @<Globals in the outer block@>@;
  252. @<Procedures and functions for about everything@>@;
  253. @<The procedure |initialize|@>
  254. begin
  255. initialize;
  256. print_ln(banner);@/
  257. @<Read the \.{.aux} file@>;
  258. @<Read and execute the \.{.bst} file@>;
  259. close_up_shop:
  260. @<Clean up and leave@>;
  261. exit_program:
  262. @^overflow in arithmetic@>
  263. @^system dependencies@>
  264. If the first character of a \PASCAL\ comment is a dollar sign,
  265. \ph\ treats the comment as a list of ``compiler directives'' that will
  266. affect the translation of this program into machine language.  The
  267. directives shown below specify full checking and inclusion of the \PASCAL\
  268. debugger when \BibTeX\ is being debugged,
  269. but they cause range checking and other
  270. redundant code to be eliminated when the production system is being generated.
  271. Arithmetic overflow will be detected in all cases.
  272. @<Compiler directives@>=
  273. @{@&$C-,A+,D-@}  {no range check, catch arithmetic overflow, no debug overhead}
  274. @!debug @{@&$C+,D+@}@+ gubed            {but turn everything on when debugging}
  275. @^bottom up@>
  276. @^gymnastics@>
  277. @^mooning@>
  278. All procedures in this program (except for |initialize|) are grouped
  279. into one of the seven classes below, and these classes are dispersed
  280. throughout the program.  However: Much of this program is written top
  281. down, yet \PASCAL\ wants its procedures bottom up.  Since mooning is
  282. neither a technically nor a socially acceptable solution to the
  283. bottom-up problem, this section instead performs the topological
  284. gymnastics that \.{WEB} allows, ordering these classes to satisfy
  285. \PASCAL\ compilers.  There are a few procedures still out of place
  286. after this ordering, though, and the other modules that complete the
  287. task have ``gymnastics'' as an index entry.
  288. @<Procedures and functions for about everything@>=
  289. @<Procedures and functions for all file I/O, error messages, and such@>@;
  290. @<Procedures and functions for file-system interacting@>@;
  291. @<Procedures and functions for handling numbers, characters, and strings@>@;
  292. @<Procedures and functions for input scanning@>@;
  293. @<Procedures and functions for name-string processing@>@;
  294. @<Procedures and functions for style-file function execution@>@;
  295. @<Procedures and functions for the reading and processing of input files@>
  296. This procedure gets things started properly.
  297. @<The procedure |initialize|@>=
  298. procedure initialize;
  299. var @<Local variables for initialization@>
  300. begin
  301. @<Check the ``constant'' values for consistency@>;
  302. if (bad > 0) then
  303.     begin
  304.     write_ln (term_out,bad:0,' is a bad bad');
  305.     goto exit_program;
  306.     end;
  307. @<Set initial values of key variables@>;
  308. pre_def_certain_strings;@/
  309. get_the_top_level_aux_file_name;
  310. @^space savings@>
  311. @^system dependencies@>
  312. These parameters can be changed at compile time to extend or reduce
  313. \BibTeX's capacity.  They are set to accommodate about 750 cites when
  314. used with the standard styles, although |pool_size| is usually the
  315. first limitation to be a problem, often when there are 500 cites.
  316. @<Constants in the outer block@>=
  317. @!buf_size=1000; {maximum number of characters in an input line (or string)}
  318. @!min_print_line=3; {minimum \.{.bbl} line length: must be |>=3|}
  319. @!max_print_line=79; {the maximum: must be |>min_print_line| and |<buf_size|}
  320. @!aux_stack_size=20; {maximum number of simultaneous open \.{.aux} files}
  321. @!max_bib_files=20; {maximum number of \.{.bib} files allowed}
  322. @!pool_size=65000; {maximum number of characters in strings}
  323. @!max_strings=4000; {maximum number of strings, including pre-defined;
  324.                                                         must be |<=hash_size|}
  325. @!max_cites=750; {maximum number of distinct cite keys; must be
  326.                                                         |<=max_strings|}
  327. @!min_crossrefs=2; {minimum number of cross-refs required for automatic
  328.                                                         |cite_list| inclusion}
  329. @!wiz_fn_space=3000; {maximum amount of |wiz_defined|-function space}
  330. @!single_fn_space=100; {maximum amount for a single |wiz_defined|-function}
  331. @!max_ent_ints=3000; {maximum number of |int_entry_var|s
  332.                                         (entries $\times$ |int_entry_var|s)}
  333. @!max_ent_strs=3000; {maximum number of |str_entry_var|s
  334.                                         (entries $\times$ |str_entry_var|s)}
  335. @!ent_str_size=100; {maximum size of a |str_entry_var|; must be |<=buf_size|}
  336. @!glob_str_size=1000; {maximum size of a |str_global_var|;
  337.                                                         must be |<=buf_size|}
  338. @!max_fields=17250; {maximum number of fields (entries $\times$ fields,
  339.                                         about |23*max_cites| for consistency)}
  340. @!lit_stk_size=100; {maximum number of literal functions on the stack}
  341. @^space savings@>
  342. @^system dependencies@>
  343. These parameters can also be changed at compile time, but they're
  344. needed to define some \.{WEB} numeric macros so they must be so
  345. defined themselves.
  346. @d hash_size=5000       {must be |>= max_strings| and |>= hash_prime|}
  347. @d hash_prime=4253      {a prime number about 85\% of |hash_size| and |>= 128|
  348.                                                 and |< @t$2^{14}-2^6$@>|}
  349. @d file_name_size=40    {file names shouldn't be longer than this}
  350. @d max_glob_strs=10     {maximum number of |str_global_var| names}
  351. @d max_glb_str_minus_1 = max_glob_strs-1  {to avoid wasting a |str_global_var|}
  352. In case somebody has inadvertently made bad settings of the ``constants,''
  353. \BibTeX\ checks them using a global variable called |bad|.
  354. This is the first of many sections of \BibTeX\ where global variables are
  355. defined.
  356. @<Globals in the outer block@>=
  357. @!bad:integer;          {is some ``constant'' wrong?}
  358. Each digit-value of |bad| has a specific meaning.
  359. @<Check the ``constant'' values for consistency@>=
  360. bad := 0;
  361. if (min_print_line < 3) then                    bad:=1;
  362. if (max_print_line <= min_print_line) then      bad:=10*bad+2;
  363. if (max_print_line >= buf_size) then            bad:=10*bad+3;
  364. if (hash_prime < 128) then                      bad:=10*bad+4;
  365. if (hash_prime > hash_size) then                bad:=10*bad+5;
  366. if (hash_prime >= (16384-64)) then              bad:=10*bad+6;
  367. if (max_strings > hash_size) then               bad:=10*bad+7;
  368. if (max_cites > max_strings) then               bad:=10*bad+8;
  369. if (ent_str_size > buf_size) then               bad:=10*bad+9;
  370. if (glob_str_size > buf_size) then              bad:=100*bad+11;
  371.                                                         {well, almost each}
  372. A global variable called |history| will contain one of four values at
  373. the end of every run: |spotless| means that no unusual messages were
  374. printed; |warning_message| means that a message of possible interest
  375. was printed but no serious errors were detected; |error_message| means
  376. that at least one error was found; |fatal_message| means that the
  377. program terminated abnormally. The value of |history| does not
  378. influence the behavior of the program; it is simply computed for the
  379. convenience of systems that might want to use such information.
  380. @d spotless=0           {|history| value for normal jobs}
  381. @d warning_message=1    {|history| value when non-serious info was printed}
  382. @d error_message=2      {|history| value when an error was noted}
  383. @d fatal_message=3      {|history| value when we had to stop prematurely}
  384. @<Procedures and functions for all file I/O, error messages, and such@>=
  385. procedure mark_warning;
  386. begin
  387. if (history = warning_message) then
  388.     incr(err_count)
  389.   else if (history = spotless) then
  390.     begin
  391.     history := warning_message;
  392.     err_count := 1;
  393.     end;
  394. procedure mark_error;
  395. begin
  396. if (history < error_message) then
  397.     begin
  398.     history := error_message;
  399.     err_count := 1;
  400.     end
  401.   else  {|history = error_message|}
  402.     incr(err_count);
  403. procedure mark_fatal;
  404. begin
  405. history := fatal_message;
  406. For the two states |warning_message| and |error_message| we keep track
  407. of the number of messages given; but since |warning_message|s aren't
  408. so serious, we ignore them once we've seen an |error_message|.  Hence
  409. we need just the single variable |err_count| to keep track.
  410. @<Globals in the outer block@>=
  411. @!history:spotless..fatal_message; {how bad was this run?}
  412. @!err_count:integer;
  413. The |err_count| gets set or reset when |history| first changes to
  414. |warning_message| or |error_message|, so we don't need to initialize
  415. @<Set initial values of key variables@>=
  416. history := spotless;
  417. @* The character set.
  418. @^ASCII code@>
  419. (The following material is copied (almost) verbatim from \TeX.
  420. Thus, the same system-dependent changes should be made to both programs.)
  421. In order to make \TeX\ readily portable between a wide variety of
  422. computers, all of its input text is converted to an internal seven-bit
  423. code that is essentially standard ASCII, the ``American Standard Code for
  424. Information Interchange.''  This conversion is done immediately when each
  425. character is read in. Conversely, characters are converted from ASCII to
  426. the user's external representation just before they are output to a
  427. text file.
  428. Such an internal code is relevant to users of \TeX\ primarily because it
  429. governs the positions of characters in the fonts. For example, the
  430. character `\.A' has ASCII code $65=@'101$, and when \TeX\ typesets
  431. this letter it specifies character number 65 in the current font.
  432. If that font actually has `\.A' in a different position, \TeX\ doesn't
  433. know what the real position is; the program that does the actual printing from
  434. \TeX's device-independent files is responsible for converting from ASCII to
  435. a particular font encoding.
  436. \TeX's internal code is relevant also with respect to constants
  437. that begin with a reverse apostrophe.
  438. Characters of text that have been converted to \TeX's internal form
  439. are said to be of type |ASCII_code|, which is a subrange of the integers.
  440. @<Types in the outer block@>=
  441. @!ASCII_code=0..127;    {seven-bit numbers}
  442. @^character set dependencies@>
  443. @^system dependencies@>
  444. The original \PASCAL\ compiler was designed in the late 60s, when six-bit
  445. character sets were common, so it did not make provision for lower-case
  446. letters. Nowadays, of course, we need to deal with both capital and small
  447. letters in a convenient way, especially in a program for typesetting;
  448. so the present specification of \TeX\ has been written under the assumption
  449. that the \PASCAL\ compiler and run-time system permit the use of text files
  450. with more than 64 distinguishable characters. More precisely, we assume that
  451. the character set contains at least the letters and symbols associated
  452. with ASCII codes @'40 through @'176; all of these characters are now
  453. available on most computer terminals.
  454. Since we are dealing with more characters than were present in the first
  455. \PASCAL\ compilers, we have to decide what to call the associated data
  456. type. Some \PASCAL s use the original name |char| for the
  457. characters in text files, even though there now are more than 64 such
  458. characters, while other \PASCAL s consider |char| to be a 64-element
  459. subrange of a larger data type that has some other name.
  460. In order to accommodate this difference, we shall use the name |text_char|
  461. to stand for the data type of the characters that are converted to and
  462. from |ASCII_code| when they are input and output. We shall also assume
  463. that |text_char| consists of the elements |chr(first_text_char)| through
  464. |chr(last_text_char)|, inclusive. The following definitions should be
  465. adjusted if necessary.
  466. @d text_char == char    {the data type of characters in text files}
  467. @d first_text_char=0    {ordinal number of the smallest element of |text_char|}
  468. @d last_text_char=127   {ordinal number of the largest element of |text_char|}
  469. @<Local variables for initialization@>=
  470. i:0..last_text_char;    {this is the first one declared}
  471. The \TeX\ processor converts between ASCII code and
  472. the user's external character set by means of arrays |xord| and |xchr|
  473. that are analogous to \PASCAL's |ord| and |chr| functions.
  474. @<Globals in the outer block@>=
  475. @!xord: array [text_char] of ASCII_code;
  476.   {specifies conversion of input characters}
  477. @!xchr: array [ASCII_code] of text_char;
  478.   {specifies conversion of output characters}
  479. @^character set dependencies@>
  480. @^system dependencies@>
  481. Since we are assuming that our \PASCAL\ system is able to read and write the
  482. visible characters of standard ASCII (although not necessarily using the
  483. ASCII codes to represent them), the following assignment statements initialize
  484. most of the |xchr| array properly, without needing any system-dependent
  485. changes. On the other hand, it is possible to implement \TeX\ with
  486. less complete character sets, and in such cases it will be necessary to
  487. change something here.
  488. @<Set initial values of key variables@>=
  489. xchr[@'40]:=' ';
  490. xchr[@'41]:='!';
  491. xchr[@'42]:='"';
  492. xchr[@'43]:='#';
  493. xchr[@'44]:='$';
  494. xchr[@'45]:='%';
  495. xchr[@'46]:='&';
  496. xchr[@'47]:='''';@/
  497. xchr[@'50]:='(';
  498. xchr[@'51]:=')';
  499. xchr[@'52]:='*';
  500. xchr[@'53]:='+';
  501. xchr[@'54]:=',';
  502. xchr[@'55]:='-';
  503. xchr[@'56]:='.';
  504. xchr[@'57]:='/';@/
  505. xchr[@'60]:='0';
  506. xchr[@'61]:='1';
  507. xchr[@'62]:='2';
  508. xchr[@'63]:='3';
  509. xchr[@'64]:='4';
  510. xchr[@'65]:='5';
  511. xchr[@'66]:='6';
  512. xchr[@'67]:='7';@/
  513. xchr[@'70]:='8';
  514. xchr[@'71]:='9';
  515. xchr[@'72]:=':';
  516. xchr[@'73]:=';';
  517. xchr[@'74]:='<';
  518. xchr[@'75]:='=';
  519. xchr[@'76]:='>';
  520. xchr[@'77]:='?';@/
  521. xchr[@'100]:='@@';
  522. xchr[@'101]:='A';
  523. xchr[@'102]:='B';
  524. xchr[@'103]:='C';
  525. xchr[@'104]:='D';
  526. xchr[@'105]:='E';
  527. xchr[@'106]:='F';
  528. xchr[@'107]:='G';@/
  529. xchr[@'110]:='H';
  530. xchr[@'111]:='I';
  531. xchr[@'112]:='J';
  532. xchr[@'113]:='K';
  533. xchr[@'114]:='L';
  534. xchr[@'115]:='M';
  535. xchr[@'116]:='N';
  536. xchr[@'117]:='O';@/
  537. xchr[@'120]:='P';
  538. xchr[@'121]:='Q';
  539. xchr[@'122]:='R';
  540. xchr[@'123]:='S';
  541. xchr[@'124]:='T';
  542. xchr[@'125]:='U';
  543. xchr[@'126]:='V';
  544. xchr[@'127]:='W';@/
  545. xchr[@'130]:='X';
  546. xchr[@'131]:='Y';
  547. xchr[@'132]:='Z';
  548. xchr[@'133]:='[';
  549. xchr[@'134]:='\';
  550. xchr[@'135]:=']';
  551. xchr[@'136]:='^';
  552. xchr[@'137]:='_';@/
  553. xchr[@'140]:='`';
  554. xchr[@'141]:='a';
  555. xchr[@'142]:='b';
  556. xchr[@'143]:='c';
  557. xchr[@'144]:='d';
  558. xchr[@'145]:='e';
  559. xchr[@'146]:='f';
  560. xchr[@'147]:='g';@/
  561. xchr[@'150]:='h';
  562. xchr[@'151]:='i';
  563. xchr[@'152]:='j';
  564. xchr[@'153]:='k';
  565. xchr[@'154]:='l';
  566. xchr[@'155]:='m';
  567. xchr[@'156]:='n';
  568. xchr[@'157]:='o';@/
  569. xchr[@'160]:='p';
  570. xchr[@'161]:='q';
  571. xchr[@'162]:='r';
  572. xchr[@'163]:='s';
  573. xchr[@'164]:='t';
  574. xchr[@'165]:='u';
  575. xchr[@'166]:='v';
  576. xchr[@'167]:='w';@/
  577. xchr[@'170]:='x';
  578. xchr[@'171]:='y';
  579. xchr[@'172]:='z';
  580. xchr[@'173]:='{';
  581. xchr[@'174]:='|';
  582. xchr[@'175]:='}';
  583. xchr[@'176]:='~';@/
  584. xchr[0]:=' '; xchr[@'177]:=' ';
  585.   {ASCII codes 0 and |@'177| do not appear in text}
  586. @^character set dependencies@>
  587. @^system dependencies@>
  588. Some of the ASCII codes without visible characters have been given symbolic
  589. names in this program because they are used with a special meaning.  The
  590. |tab| character may be system dependent.
  591. @d null_code=@'0        {ASCII code that might disappear}
  592. @d tab=@'11             {ASCII code treated as |white_space|}
  593. @d space=@'40           {ASCII code treated as |white_space|}
  594. @d invalid_code=@'177   {ASCII code that should not appear}
  595. @^character set dependencies@>
  596. @^system dependencies@>
  597. @:TeXbook}{\sl The \TeX book@>
  598. The ASCII code is ``standard'' only to a certain extent, since many
  599. computer installations have found it advantageous to have ready access
  600. to more than 94 printing characters. Appendix~C of {\sl The \TeX book\/}
  601. gives a complete specification of the intended correspondence between
  602. characters and \TeX's internal representation.
  603. If \TeX\ is being used
  604. on a garden-variety \PASCAL\ for which only standard ASCII
  605. codes will appear in the input and output files, it doesn't really matter
  606. what codes are specified in |xchr[1..@'37]|, but the safest policy is to
  607. blank everything out by using the code shown below.
  608. However, other settings of |xchr| will make \TeX\ more friendly on
  609. computers that have an extended character set, so that users can type things
  610. like `\.^^Z' instead of `\.{\\ne}'. At MIT, for example, it would be more
  611. appropriate to substitute the code
  612. $$\hbox{|for i:=1 to @'37 do xchr[i]:=chr(i);|}$$
  613. \TeX's character set is essentially the same as MIT's, even with respect to
  614. characters less than~@'40. People with extended character sets can
  615. assign codes arbitrarily, giving an |xchr| equivalent to whatever
  616. characters the users of \TeX\ are allowed to have in their input files.
  617. It is best to make the codes correspond to the intended interpretations as
  618. shown in Appendix~C whenever possible; but this is not necessary. For
  619. example, in countries with an alphabet of more than 26 letters, it is
  620. usually best to map the additional letters into codes less than~@'40.
  621. @<Set initial values of key variables@>=
  622. for i:=1 to @'37 do xchr[i]:=' ';
  623. xchr[tab]:=chr(tab);
  624. This system-independent code makes the |xord| array contain a suitable
  625. inverse to the information in |xchr|. Note that if |xchr[i]=xchr[j]|
  626. where |i<j<@'177|, the value of |xord[xchr[i]]| will turn out to be
  627. |j| or more; hence, standard ASCII code numbers will be used instead
  628. of codes below @'40 in case there is a coincidence.
  629. @<Set initial values of key variables@>=
  630. for i:=first_text_char to last_text_char do xord[chr(i)]:=invalid_code;
  631. for i:=1 to @'176 do xord[xchr[i]]:=i;
  632. Also, various characters are given symbolic names; all the ones this
  633. program uses are collected here.  We use the sharp sign as the
  634. |concat_char|, rather than something more natural (like an ampersand),
  635. for uniformity of database syntax (ampersand is a valid character in
  636. identifiers).
  637. @d double_quote = """"          {delimits strings}
  638. @d number_sign = "#"            {marks an |int_literal|}
  639. @d comment = "%"                {ignore the rest of a \.{.bst} or \TeX\ line}
  640. @d single_quote = "'"           {marks a quoted function}
  641. @d left_paren = "("             {optional database entry left delimiter}
  642. @d right_paren = ")"            {corresponding right delimiter}
  643. @d comma = ","                  {separates various things}
  644. @d minus_sign = "-"             {for a negative number}
  645. @d equals_sign = "="            {separates a field name from a field value}
  646. @d at_sign = "@@"               {the beginning of a database entry}
  647. @d left_brace = "{"             {left delimiter of many things}
  648. @d right_brace = "}"            {corresponding right delimiter}
  649. @d period = "."                 {these are three}
  650. @d question_mark = "?"          {string-ending characters}
  651. @d exclamation_mark = "!"       {of interest in \.{add.period\$}}
  652. @d tie = "~"                    {the default space char, in \.{format.name\$}}
  653. @d hyphen = "-"                 {like |white_space|, in \.{format.name\$}}
  654. @d star = "*"                   {for including entire database}
  655. @d concat_char = "#"            {for concatenating field tokens}
  656. @d colon = ":"                  {for lower-casing (usually title) strings}
  657. @d backslash = "\"              {used to recognize accented characters}
  658. These arrays give a lexical classification for the |ASCII_code|s;
  659. |lex_class| is used for general scanning and |id_class| is used for
  660. scanning identifiers.
  661. @<Globals in the outer block@>=
  662. @!lex_class: array [ASCII_code] of lex_type;
  663. @!id_class: array [ASCII_code] of id_type;
  664. Every character has two types of the lexical classifications.  The
  665. first type is general, and the second type tells whether the character
  666. is legal in identifiers.
  667. @d illegal = 0          {the unrecognized |ASCII_code|s}
  668. @d white_space = 1      {things like |space|s that you can't see}
  669. @d alpha = 2            {the upper- and lower-case letters}
  670. @d numeric = 3          {the ten digits}
  671. @d sep_char = 4         {things sometimes treated like |white_space|}
  672. @d other_lex = 5        {when none of the above applies}
  673. @d last_lex = 5         {the same number as on the line above}
  674. @d illegal_id_char = 0  {a few forbidden ones}
  675. @d legal_id_char = 1    {most printing characters}
  676. @<Types in the outer block@>=
  677. @!lex_type = 0..last_lex;@/
  678. @!id_type = 0..1;
  679. @^character set dependencies@>
  680. @^system dependencies@>
  681. Now we initialize the system-dependent |lex_class| array.  The |tab|
  682. character may be system dependent.  Note that the order of these
  683. assignments is important here.
  684. @<Set initial values of key variables@>=
  685. for i:=0 to @'177 do lex_class[i] := other_lex;
  686. for i:=0 to @'37 do lex_class[i] := illegal;
  687. lex_class[invalid_code] := illegal;
  688. lex_class[tab] := white_space;
  689. lex_class[space] := white_space;
  690. lex_class[tie] := sep_char;
  691. lex_class[hyphen] := sep_char;
  692. for i:=@'60 to @'71 do lex_class[i] := numeric;
  693. for i:=@'101 to @'132 do lex_class[i] := alpha;
  694. for i:=@'141 to @'172 do lex_class[i] := alpha;
  695. @^character set dependencies@>
  696. @^system dependencies@>
  697. And now the |id_class| array.
  698. @<Set initial values of key variables@>=
  699. for i:=0 to @'177 do id_class[i] := legal_id_char;
  700. for i:=0 to @'37 do id_class[i] := illegal_id_char;
  701. id_class[space] := illegal_id_char;
  702. id_class[tab] := illegal_id_char;
  703. id_class[double_quote] := illegal_id_char;
  704. id_class[number_sign] := illegal_id_char;
  705. id_class[comment] := illegal_id_char;
  706. id_class[single_quote] := illegal_id_char;
  707. id_class[left_paren] := illegal_id_char;
  708. id_class[right_paren] := illegal_id_char;
  709. id_class[comma] := illegal_id_char;
  710. id_class[equals_sign] := illegal_id_char;
  711. id_class[left_brace] := illegal_id_char;
  712. id_class[right_brace] := illegal_id_char;
  713. The array |char_width| gives relative printing widths of each
  714. |ASCII_code|, and |string_width| will be used later to sum up
  715. |char_width|s in a string.
  716. @<Globals in the outer block@>=
  717. @!char_width : array [ASCII_code] of integer;
  718. @!string_width : integer;
  719. @^character set dependencies@>
  720. @^system dependencies@>
  721. Now we initialize the system-dependent |char_width| array, for which
  722. |space| is the only |white_space| character given a nonzero printing
  723. width.  The widths here are taken from Stanford's June~'87
  724. $cmr10$~font and represent hundredths of a point (rounded), but since
  725. they're used only for relative comparisons, the units have no meaning.
  726. @d ss_width = 500               {character |@'31|'s width in the $cmr10$ font}
  727. @d ae_width = 722               {character |@'32|'s width in the $cmr10$ font}
  728. @d oe_width = 778               {character |@'33|'s width in the $cmr10$ font}
  729. @d upper_ae_width = 903         {character |@'35|'s width in the $cmr10$ font}
  730. @d upper_oe_width = 1014        {character |@'36|'s width in the $cmr10$ font}
  731. @<Set initial values of key variables@>=
  732. for i:=0 to @'177 do char_width[i] := 0;
  733. char_width[@'40] := 278;
  734. char_width[@'41] := 278;
  735. char_width[@'42] := 500;
  736. char_width[@'43] := 833;
  737. char_width[@'44] := 500;
  738. char_width[@'45] := 833;
  739. char_width[@'46] := 778;
  740. char_width[@'47] := 278;
  741. char_width[@'50] := 389;
  742. char_width[@'51] := 389;
  743. char_width[@'52] := 500;
  744. char_width[@'53] := 778;
  745. char_width[@'54] := 278;
  746. char_width[@'55] := 333;
  747. char_width[@'56] := 278;
  748. char_width[@'57] := 500;
  749. char_width[@'60] := 500;
  750. char_width[@'61] := 500;
  751. char_width[@'62] := 500;
  752. char_width[@'63] := 500;
  753. char_width[@'64] := 500;
  754. char_width[@'65] := 500;
  755. char_width[@'66] := 500;
  756. char_width[@'67] := 500;
  757. char_width[@'70] := 500;
  758. char_width[@'71] := 500;
  759. char_width[@'72] := 278;
  760. char_width[@'73] := 278;
  761. char_width[@'74] := 278;
  762. char_width[@'75] := 778;
  763. char_width[@'76] := 472;
  764. char_width[@'77] := 472;
  765. char_width[@'100] := 778;
  766. char_width[@'101] := 750;
  767. char_width[@'102] := 708;
  768. char_width[@'103] := 722;
  769. char_width[@'104] := 764;
  770. char_width[@'105] := 681;
  771. char_width[@'106] := 653;
  772. char_width[@'107] := 785;
  773. char_width[@'110] := 750;
  774. char_width[@'111] := 361;
  775. char_width[@'112] := 514;
  776. char_width[@'113] := 778;
  777. char_width[@'114] := 625;
  778. char_width[@'115] := 917;
  779. char_width[@'116] := 750;
  780. char_width[@'117] := 778;
  781. char_width[@'120] := 681;
  782. char_width[@'121] := 778;
  783. char_width[@'122] := 736;
  784. char_width[@'123] := 556;
  785. char_width[@'124] := 722;
  786. char_width[@'125] := 750;
  787. char_width[@'126] := 750;
  788. char_width[@'127] :=1028;
  789. char_width[@'130] := 750;
  790. char_width[@'131] := 750;
  791. char_width[@'132] := 611;
  792. char_width[@'133] := 278;
  793. char_width[@'134] := 500;
  794. char_width[@'135] := 278;
  795. char_width[@'136] := 500;
  796. char_width[@'137] := 278;
  797. char_width[@'140] := 278;
  798. char_width[@'141] := 500;
  799. char_width[@'142] := 556;
  800. char_width[@'143] := 444;
  801. char_width[@'144] := 556;
  802. char_width[@'145] := 444;
  803. char_width[@'146] := 306;
  804. char_width[@'147] := 500;
  805. char_width[@'150] := 556;
  806. char_width[@'151] := 278;
  807. char_width[@'152] := 306;
  808. char_width[@'153] := 528;
  809. char_width[@'154] := 278;
  810. char_width[@'155] := 833;
  811. char_width[@'156] := 556;
  812. char_width[@'157] := 500;
  813. char_width[@'160] := 556;
  814. char_width[@'161] := 528;
  815. char_width[@'162] := 392;
  816. char_width[@'163] := 394;
  817. char_width[@'164] := 389;
  818. char_width[@'165] := 556;
  819. char_width[@'166] := 528;
  820. char_width[@'167] := 722;
  821. char_width[@'170] := 528;
  822. char_width[@'171] := 528;
  823. char_width[@'172] := 444;
  824. char_width[@'173] := 500;
  825. char_width[@'174] :=1000;
  826. char_width[@'175] := 500;
  827. char_width[@'176] := 500;
  828. @* Input and output.
  829. The basic operations we need to do are
  830. (1)~inputting and outputting of text characters to or from a file;
  831. (2)~instructing the operating system to initiate (``open'')
  832. or to terminate (``close'') input or output to or from a specified file; and
  833. (3)~testing whether the end of an input file has been reached.
  834. @<Types in the outer block@>=
  835. @!alpha_file=packed file of text_char;  {files that contain textual data}
  836. @^system dependencies@>
  837. Most of what we need to do with respect to input and output can be handled
  838. by the I/O facilities that are standard in \PASCAL, i.e., the routines
  839. called |get|, |put|, |eof|, and so on. But
  840. standard \PASCAL\ does not allow file variables to be associated with file
  841. names that are determined at run time, so it cannot be used to implement
  842. \BibTeX; some sort of extension to \PASCAL's ordinary |reset| and |rewrite|
  843. is crucial for our purposes. We shall assume that |name_of_file| is a variable
  844. of an appropriate type such that the \PASCAL\ run-time system being used to
  845. implement \BibTeX\ can open a file whose external name is specified by
  846. |name_of_file|. \BibTeX\ does no case conversion for file names.
  847. @<Globals in the outer block@>=
  848. @!name_of_file:packed array[1..file_name_size] of char;
  849.                          {on some systems this is a \&{record} variable}
  850. @!name_length:0..file_name_size;
  851.   {this many characters are relevant in |name_of_file| (the rest are blank)}
  852. @!name_ptr:0..file_name_size+1;         {index variable into |name_of_file|}
  853. @^system dependencies@>
  854. @:PASCAL H}{\ph@>
  855. The \ph\ compiler with which the present version of \TeX\ was prepared has
  856. extended the rules of \PASCAL\ in a very convenient way. To open file~|f|,
  857. we can write
  858. $$\vbox{\halign{#\hfil\qquad&#\hfil\cr
  859. |reset(f,@t\\{name}@>,'/O')|&for input;\cr
  860. |rewrite(f,@t\\{name}@>,'/O')|&for output.\cr}}$$
  861. The `\\{name}' parameter, which is of type `\ignorespaces|packed
  862. array[@t\<\\{any}>@>] of text_char|', stands for the name of
  863. the external file that is being opened for input or output.
  864. Blank spaces that might appear in \\{name} are ignored.
  865. The `\.{/O}' parameter tells the operating system not to issue its own
  866. error messages if something goes wrong. If a file of the specified name
  867. cannot be found, or if such a file cannot be opened for some other reason
  868. (e.g., someone may already be trying to write the same file), we will have
  869. |@!erstat(f)<>0| after an unsuccessful |reset| or |rewrite|.  This allows
  870. \TeX\ to undertake appropriate corrective action.
  871. \TeX's file-opening procedures return |false| if no file identified by
  872. |name_of_file| could be opened.
  873. @d reset_OK(#)==erstat(#)=0
  874. @d rewrite_OK(#)==erstat(#)=0
  875. @<Procedures and functions for file-system interacting@>=
  876. function erstat(var f:file):integer; extern;    {in the runtime library}
  877. @#@t\2@>
  878. function a_open_in(var f:alpha_file):boolean;   {open a text file for input}
  879. begin reset(f,name_of_file,'/O'); a_open_in:=reset_OK(f);
  880. function a_open_out(var f:alpha_file):boolean;  {open a text file for output}
  881. begin rewrite(f,name_of_file,'/O'); a_open_out:=rewrite_OK(f);
  882. @^system dependencies@>
  883. Files can be closed with the \ph\ routine `|close(f)|', which should
  884. be used when all input or output with respect to |f| has been
  885. completed.  This makes |f| available to be opened again, if desired;
  886. and if |f| was used for output, the |close| operation makes the
  887. corresponding external file appear on the user's area, ready to be
  888. read.
  889. @<Procedures and functions for file-system interacting@>=
  890. procedure a_close(var f:alpha_file);            {close a text file}
  891. begin close(f);
  892. Text output is easy to do with the ordinary \PASCAL\ |put| procedure,
  893. so we don't have to make any other special arrangements.
  894. The treatment of text input is more difficult, however, because
  895. of the necessary translation to |ASCII_code| values, and because
  896. \TeX's conventions should be efficient and they should
  897. blend nicely with the user's operating environment.
  898. Input from text files is read one line at a time, using a routine
  899. called |input_ln|. This function is defined in terms of global
  900. variables called |buffer| and |last|.  The |buffer| array contains
  901. |ASCII_code| values, and |last| is an index into this array marking
  902. the end of a line of text.  (Occasionally, |buffer| is used for
  903. something else, in which case it is copied to a temporary array.)
  904. @<Globals in the outer block@>=
  905. @!buffer:buf_type;      {usually, lines of characters being read}
  906. @!last:buf_pointer;     {end of the line just input to |buffer|}
  907. @^save space@>
  908. @^space savings@>
  909. @^system dependencies@>
  910. The type |buf_type| is used for |buffer|, for saved copies of it, or
  911. for scratch work.  It's not |packed| because otherwise the program
  912. would run much slower on some systems (more than 25 percent slower,
  913. for example, on a TOPS-20 operating system).  But on systems that are
  914. byte-addressable and that have a good compiler, packing |buf_type|
  915. would save lots of space without much loss of speed.  Other modules
  916. that have packable arrays are also marked with a ``space savings''
  917. index entry.
  918. @<Types in the outer block@>=
  919. @!buf_pointer = 0..buf_size;                    {an index into a |buf_type|}
  920. @!buf_type = array[buf_pointer] of ASCII_code;  {for various buffers}
  921. @^kludge@>
  922. And while we're at it, we declare another buffer for general use.
  923. Because buffers are not packed and can get large, we use |sv_buffer|
  924. several purposes; this is a bit kludgy, but it helps make the stack
  925. space not overflow on some machines.  It's used when reading the
  926. entire database file (in the \.{read} command) and when doing
  927. name-handling (through the alias |name_buf|) in the |built_in|
  928. functions \.{format.names\$} and \.{num.names\$}.
  929. @<Globals in the outer block@>=
  930. @!sv_buffer : buf_type;
  931. @!sv_ptr1 : buf_pointer;
  932. @!sv_ptr2 : buf_pointer;
  933. @!tmp_ptr,@!tmp_end_ptr : integer; {copy pointers only, usually for buffers}
  934. @.BibTeX capacity exceeded@>
  935. When something in the program wants to be bigger or something out
  936. there wants to be smaller, it's time to call it a run.  Here's the
  937. first of several macros that have associated procedures so that they
  938. produce less inline code.
  939. @d overflow(#)==begin           {fatal error---close up shop}
  940.                 print_overflow;
  941.                 print_ln(#:0);
  942.                 goto close_up_shop;
  943.                 end
  944. @<Procedures and functions for all file I/O, error messages, and such@>=
  945. procedure print_overflow;
  946. begin
  947. print ('Sorry---you''ve exceeded BibTeX''s ');
  948. mark_fatal;
  949. @.this can't happen@>
  950. When something happens that the program thinks is impossible,
  951. call the maintainer.
  952. @d confusion(#)==begin          {fatal error---close up shop}
  953.                  print (#);
  954.                  print_confusion;
  955.                  goto close_up_shop;
  956.                  end
  957. @<Procedures and functions for all file I/O, error messages, and such@>=
  958. procedure print_confusion;
  959. begin
  960. print_ln ('---this can''t happen');
  961. print_ln ('*Please notify the BibTeX maintainer*');
  962. mark_fatal;
  963. @:BibTeX capacity exceeded}{\quad buffer size@>
  964. When a buffer overflows, it's time to complain (and then quit).
  965. @<Procedures and functions for all file I/O, error messages, and such@>=
  966. procedure buffer_overflow;
  967. begin
  968. overflow('buffer size ',buf_size);
  969. @:BibTeX capacity exceeded}{\quad buffer size@>
  970. The |input_ln| function brings the next line of input from the
  971. specified file into available positions of the buffer array and
  972. returns the value |true|, unless the file has already been entirely
  973. read, in which case it returns |false| and sets |last:=0|.  In
  974. general, the |ASCII_code| numbers that represent the next line of the
  975. file are input into |buffer[0]|, |buffer[1]|, \dots, |buffer[last-1]|;
  976. and the global variable |last| is set equal to the length of the line.
  977. Trailing |white_space| characters are removed from the line
  978. (|white_space| characters are explained in the character-set section%
  979. ---most likely they're blanks); thus, either |last=0| (in which case
  980. the line was entirely blank) or |lex_class[buffer[last-1]]<>white_space|.
  981. An overflow error is given if the normal actions of |input_ln| would
  982. make |last>buf_size|.
  983. Standard \PASCAL\ says that a file should have |eoln| immediately
  984. before |eof|, but \BibTeX\ needs only a weaker restriction: If |eof|
  985. occurs in the middle of a line, the system function |eoln| should return
  986. a |true| result (even though |f^| will be undefined).
  987. @<Procedures and functions for all file I/O, error messages, and such@>=
  988. function input_ln(var f:alpha_file) : boolean;
  989.                                 {inputs the next line or returns |false|}
  990. label loop_exit;
  991. begin
  992. last:=0;
  993. if (eof(f)) then input_ln:=false
  994.   begin
  995.   while (not eoln(f)) do
  996.     begin
  997.     if (last >= buf_size) then
  998.         buffer_overflow;
  999.     buffer[last]:=xord[f^];
  1000.     get(f); incr(last);
  1001.     end;
  1002.   get(f);
  1003.   while (last > 0) do           {remove trailing |white_space|}
  1004.     if (lex_class[buffer[last-1]] = white_space) then
  1005.       decr(last)
  1006.      else
  1007.       goto loop_exit;
  1008. loop_exit:
  1009.   input_ln:=true;
  1010.   end;
  1011. @* String handling.
  1012. \BibTeX\ uses variable-length strings of seven-bit characters.
  1013. Since \PASCAL\ does not have a well-developed string mechanism,
  1014. \BibTeX\ does all its string processing by home-grown
  1015. (predominantly \TeX's) methods.
  1016. Unlike \TeX, however, \BibTeX\ does not use a |pool_file| for
  1017. string storage; it creates its few pre-defined strings at run-time.
  1018. The necessary operations are handled with a simple data structure.
  1019. The array |str_pool| contains all the (seven-bit) ASCII codes in all
  1020. the strings \BibTeX\ must ever search for (generally identifiers
  1021. names), and the array |str_start| contains indices of the starting
  1022. points of each such string. Strings are referred to by integer
  1023. numbers, so that string number |s| comprises the characters
  1024. |str_pool[j]| for |str_start[s]<=j<str_start[s+1]|. Additional integer
  1025. variables |pool_ptr| and |str_ptr| indicate the number of entries used
  1026. so far in |str_pool| and |str_start|; locations |str_pool[pool_ptr]|
  1027. and |str_start[str_ptr]| are ready for the next string to be
  1028. allocated.  Location |str_start[0]| is unused so that hashing will
  1029. work correctly.
  1030. Elements of the |str_pool| array must be ASCII codes that can actually be
  1031. printed; i.e., they must have an |xchr| equivalent in the local
  1032. character set.
  1033. @<Globals in the outer block@>=
  1034. @!str_pool : packed array[pool_pointer] of ASCII_code;  {the characters}
  1035. @!str_start : packed array[str_number] of pool_pointer; {the starting pointers}
  1036. @!pool_ptr : pool_pointer;      {first unused position in |str_pool|}
  1037. @!str_ptr : str_number;         {start of the current string being created}
  1038. @!str_num : str_number;         {general index variable into |str_start|}
  1039. @!p_ptr1,@!p_ptr2 : pool_pointer;       {several procedures use these locally}
  1040. Where |pool_pointer| and |str_number| are pointers into |str_pool| and
  1041. |str_start|.
  1042. @<Types in the outer block@>=
  1043. @!pool_pointer = 0..pool_size;  {for variables that point into |str_pool|}
  1044. @!str_number = 0..max_strings;  {for variables that point into |str_start|}
  1045. These macros send a string in |str_pool| to an output file.
  1046. @d max_pop = 3  {---see the |built_in| functions section}
  1047. @d print_pool_str(#) == print_a_pool_str(#)
  1048.                                 {making this a procedure saves a little space}
  1049. @d trace_pr_pool_str(#) == begin
  1050.                            out_pool_str(log_file,#);
  1051.                            end
  1052. @^kludge@>
  1053. @^system dependencies@>
  1054. @:this can't happen}{\quad Illegal string number@>
  1055. And here are the associated procedures.  Note: The |term_out| file is
  1056. system dependent.
  1057. @<Procedures and functions for all file I/O, error messages, and such@>=
  1058. procedure out_pool_str (var f:alpha_file; @!s:str_number);
  1059. var i:pool_pointer;
  1060. begin   {allowing |str_ptr <= s < str_ptr+max_pop| is a \.{.bst}-stack kludge}
  1061. if ((s<0) or (s>=str_ptr+max_pop) or (s>=max_strings)) then
  1062.     confusion ('Illegal string number:',s:0);
  1063. for i := str_start[s] to str_start[s+1]-1 do
  1064.     write(f,xchr[str_pool[i]]);
  1065. procedure print_a_pool_str (@!s:str_number);
  1066. begin
  1067. out_pool_str(term_out,s);
  1068. out_pool_str(log_file,s);
  1069. @.WEB@>
  1070. Several of the elementary string operations are performed using \.{WEB}
  1071. macros instead of using \PASCAL\ procedures, because many of the
  1072. operations are done quite frequently and we want to avoid the
  1073. overhead of procedure calls. For example, here is
  1074. a simple macro that computes the length of a string.
  1075. @d length(#) == (str_start[#+1]-str_start[#])
  1076.                         {the number of characters in string number \#}
  1077. @:BibTeX capacity exceeded}{\quad pool size@>
  1078. Strings are created by appending character codes to |str_pool|.
  1079. The macro called |append_char|, defined here, does not check to see if the
  1080. value of |pool_ptr| has gotten too high; this test is supposed to be
  1081. made before |append_char| is used.
  1082. To test if there is room to append |l| more characters to |str_pool|,
  1083. we shall write |str_room(l)|, which aborts \BibTeX\ and gives an
  1084. error message if there isn't enough room.
  1085. @d append_char(#) ==            {put |ASCII_code| \# at the end of |str_pool|}
  1086. begin str_pool[pool_ptr]:=#; incr(pool_ptr);
  1087. @d str_room(#) ==               {make sure that the pool hasn't overflowed}
  1088.   begin
  1089.   if (pool_ptr+# > pool_size) then
  1090.       pool_overflow;
  1091.   end
  1092. @<Procedures and functions for all file I/O, error messages, and such@>=
  1093. procedure pool_overflow;
  1094. begin
  1095. overflow('pool size ',pool_size);
  1096. @:BibTeX capacity exceeded}{\quad number of strings@>
  1097. Once a sequence of characters has been appended to |str_pool|, it
  1098. officially becomes a string when the function |make_string| is called.
  1099. It returns the string number of the string it just made.
  1100. @<Procedures and functions for handling numbers, characters, and strings@>=
  1101. function make_string : str_number;      {current string enters the pool}
  1102. begin
  1103. if (str_ptr=max_strings) then
  1104.     overflow('number of strings ',max_strings);
  1105. incr(str_ptr);
  1106. str_start[str_ptr]:=pool_ptr;
  1107. make_string := str_ptr - 1;
  1108. These macros destroy and recreate the string at the end of the pool.
  1109. @d flush_string == begin
  1110.                    decr(str_ptr);
  1111.                    pool_ptr := str_start[str_ptr];
  1112.                    end
  1113. @d unflush_string == begin
  1114.                      incr(str_ptr);
  1115.                      pool_ptr := str_start[str_ptr];
  1116.                      end
  1117. This subroutine compares string |s| with another string that appears
  1118. in the buffer |buf| between positions |bf_ptr| and |bf_ptr+len-1|; the
  1119. result is |true| if and only if the strings are equal.
  1120. @<Procedures and functions for handling numbers, characters, and strings@>=
  1121. function str_eq_buf (@!s:str_number; var buf:buf_type;
  1122.                                         @!bf_ptr,@!len:buf_pointer) : boolean;
  1123.   {test equality of strings}
  1124. label exit;
  1125. var i : buf_pointer;    {running}
  1126. @!j : pool_pointer;     {indices}
  1127. begin
  1128. if (length(s) <> len) then      {strings of unequal length}
  1129.     begin
  1130.     str_eq_buf := false;
  1131.     return;
  1132.     end;
  1133. i := bf_ptr;
  1134. j := str_start[s];
  1135. while (j < str_start[s+1]) do
  1136.     begin
  1137.     if (str_pool[j] <> buf[i]) then
  1138.         begin
  1139.         str_eq_buf := false;
  1140.         return;
  1141.         end;
  1142.     incr(i);
  1143.     incr(j);
  1144.     end;
  1145. str_eq_buf := true;
  1146. exit:
  1147. This subroutine compares two |str_pool| strings and returns true
  1148. |true| if and only if the strings are equal.
  1149. @<Procedures and functions for handling numbers, characters, and strings@>=
  1150. function str_eq_str (@!s1,@!s2:str_number) : boolean;
  1151. label exit;
  1152. begin
  1153. if (length(s1) <> length(s2)) then
  1154.     begin
  1155.     str_eq_str := false;
  1156.     return;
  1157.     end;
  1158. p_ptr1 := str_start[s1];
  1159. p_ptr2 := str_start[s2];
  1160. while (p_ptr1 < str_start[s1+1]) do
  1161.     begin
  1162.     if (str_pool[p_ptr1] <> str_pool[p_ptr2]) then
  1163.         begin
  1164.         str_eq_str := false;
  1165.         return;
  1166.         end;
  1167.     incr(p_ptr1);
  1168.     incr(p_ptr2);
  1169.     end;
  1170. str_eq_str:=true;
  1171. exit:
  1172. @:BibTeX capacity exceeded}{\quad file name size@>
  1173. This procedure copies file name |file_name| into the beginning of
  1174. |name_of_file|, if it will fit.  It also sets the global variable
  1175. |name_length| to the appropriate value.
  1176. @<Procedures and functions for file-system interacting@>=
  1177. procedure start_name (@!file_name:str_number);
  1178. var p_ptr: pool_pointer;        {running index}
  1179. begin
  1180. if (length(file_name) > file_name_size) then
  1181.     begin
  1182.     print ('File=');
  1183.     print_pool_str (file_name);
  1184.     print_ln (',');
  1185.     file_nm_size_overflow;
  1186.     end;
  1187. name_ptr := 1;
  1188. p_ptr := str_start[file_name];
  1189. while (p_ptr < str_start[file_name+1]) do
  1190.     begin
  1191.     name_of_file[name_ptr] := chr (str_pool[p_ptr]);
  1192.     incr(name_ptr); incr(p_ptr);
  1193.     end;
  1194. name_length := length(file_name);
  1195. @:BibTeX capacity exceeded}{\quad file name size@>
  1196. Yet another complaint-before-quiting.
  1197. @<Procedures and functions for all file I/O, error messages, and such@>=
  1198. procedure file_nm_size_overflow;
  1199. begin
  1200. overflow('file name size ',file_name_size);
  1201. @:BibTeX capacity exceeded}{\quad file name size@>
  1202. This procedure copies file extension |ext| into the array
  1203. |name_of_file| starting at position |name_length+1|.  It also sets the
  1204. global variable |name_length| to the appropriate value.
  1205. @<Procedures and functions for file-system interacting@>=
  1206. procedure add_extension(@!ext:str_number);
  1207. var p_ptr: pool_pointer;        {running index}
  1208. begin
  1209. if (name_length + length(ext) > file_name_size) then
  1210.     begin
  1211.     print ('File=',name_of_file,', extension=');
  1212.     print_pool_str (ext); print_ln (',');
  1213.     file_nm_size_overflow;
  1214.     end;
  1215. name_ptr := name_length + 1;
  1216. p_ptr := str_start[ext];
  1217. while (p_ptr < str_start[ext+1]) do
  1218.     begin
  1219.     name_of_file[name_ptr] := chr (str_pool[p_ptr]);
  1220.     incr(name_ptr); incr(p_ptr);
  1221.     end;
  1222. name_length := name_length + length(ext);
  1223. name_ptr := name_length+1;
  1224. while (name_ptr <= file_name_size) do   {pad with blanks}
  1225.     begin
  1226.     name_of_file[name_ptr] := ' ';
  1227.     incr(name_ptr);
  1228.     end;
  1229. @:BibTeX capacity exceeded}{\quad file name size@>
  1230. This procedure copies the default logical area name |area| into the
  1231. array |name_of_file| starting at position 1, after shifting up the
  1232. rest of the filename.  It also sets the global variable |name_length|
  1233. to the appropriate value.
  1234. @<Procedures and functions for file-system interacting@>=
  1235. procedure add_area(@!area:str_number);
  1236. var p_ptr: pool_pointer;        {running index}
  1237. begin
  1238. if (name_length + length(area) > file_name_size) then
  1239.     begin
  1240.     print ('File=');
  1241.     print_pool_str (area); print (name_of_file,',');
  1242.     file_nm_size_overflow;
  1243.     end;
  1244. name_ptr := name_length;
  1245. while (name_ptr > 0) do         {shift up name}
  1246.     begin
  1247.     name_of_file[name_ptr+length(area)] := name_of_file[name_ptr];
  1248.     decr(name_ptr);
  1249.     end;
  1250. name_ptr := 1;
  1251. p_ptr := str_start[area];
  1252. while (p_ptr < str_start[area+1]) do
  1253.     begin
  1254.     name_of_file[name_ptr] := chr (str_pool[p_ptr]);
  1255.     incr(name_ptr); incr(p_ptr);
  1256.     end;
  1257. name_length := name_length + length(area);
  1258. This system-independent procedure converts upper-case characters to
  1259. lower case for the specified part of |buf|.  It is system independent
  1260. because it uses only the internal representation for characters.
  1261. @d case_difference = "a" - "A"
  1262. @<Procedures and functions for handling numbers, characters, and strings@>=
  1263. procedure lower_case (var buf:buf_type; @!bf_ptr,@!len:buf_pointer);
  1264. var i:buf_pointer;
  1265. begin
  1266. if (len > 0) then
  1267.   for i := bf_ptr to bf_ptr+len-1 do
  1268.     if ((buf[i]>="A") and (buf[i]<="Z")) then
  1269.         buf[i] := buf[i] + case_difference;
  1270. This system-independent procedure is the same as the previous except
  1271. that it converts lower- to upper-case letters.
  1272. @<Procedures and functions for handling numbers, characters, and strings@>=
  1273. procedure upper_case (var buf:buf_type; @!bf_ptr,@!len:buf_pointer);
  1274. var i:buf_pointer;
  1275. begin
  1276. if (len > 0) then
  1277.   for i := bf_ptr to bf_ptr+len-1 do
  1278.     if ((buf[i]>="a") and (buf[i]<="z")) then
  1279.         buf[i] := buf[i] - case_difference;
  1280. @* The hash table.
  1281. All static strings that \BibTeX\ might have to search for, generally
  1282. identifiers, are stored and retrieved by means of a fairly standard
  1283. hash-table algorithm (but slightly altered here) called the method of
  1284. ``coalescing lists''
  1285. (cf.\ Algorithm 6.4C in {\sl The Art of Computer Programming}).
  1286. Once a string enters the table, it is never removed.  The actual
  1287. sequence of characters forming a string is stored in the |str_pool|
  1288. array.
  1289. The hash table consists of the four arrays |hash_next|, |hash_text|,
  1290. |hash_ilk|, and |ilk_info|.  The first array, |hash_next[p]|, points
  1291. to the next identifier belonging to the same coalesced list as the
  1292. identifier corresponding to~|p|.  The second, |hash_text[p]|, points
  1293. to the |str_start| entry for |p|'s string. If position~|p| of the hash
  1294. table is empty, we have |hash_text[p]=0|; if position |p| is either
  1295. empty or the end of a coalesced hash list, we have
  1296. |hash_next[p]=empty|; an auxiliary pointer variable called |hash_used|
  1297. is maintained in such a way that all locations |p>=hash_used| are
  1298. nonempty.  The third, |hash_ilk[p]|, tells how this string is used (as
  1299. ordinary text, as a variable name, as an \.{.aux} file command, etc).
  1300. The fourth, |ilk_info[p]|, contains information specific to the
  1301. corresponding |hash_ilk|---for |integer_ilk|s: the integer's value;
  1302. for |cite_ilk|s: a pointer into |cite_list|; for |lc_cite_ilk|s: a
  1303. pointer to a |cite_ilk| string; for |command_ilk|s: a constant to be
  1304. used in a |case| statement; for |bst_fn_ilk|s: function-specific
  1305. information; for |macro_ilk|s: a pointer to its definition string; for
  1306. |control_seq_ilk|s: a constant for use in a |case| statement; for all
  1307. other |ilk|s it contains no information.  This |ilk|-specific
  1308. information is set in other parts of the program rather than here in
  1309. the hashing routine.
  1310. @d hash_base = empty + 1                {lowest numbered hash-table location}
  1311. @d hash_max = hash_base + hash_size - 1 {highest numbered hash-table location}
  1312. @d hash_is_full == (hash_used=hash_base) {test if all positions are occupied}
  1313. @d text_ilk = 0         {a string of ordinary text}
  1314. @d integer_ilk = 1      {an integer (possibly with a |minus_sign|)}
  1315. @d aux_command_ilk = 2  {an \.{.aux}-file command}
  1316. @d aux_file_ilk = 3     {an \.{.aux} file name}
  1317. @d bst_command_ilk = 4  {a \.{.bst}-file command}
  1318. @d bst_file_ilk = 5     {a \.{.bst} file name}
  1319. @d bib_file_ilk = 6     {a \.{.bib} file name}
  1320. @d file_ext_ilk = 7     {one of \.{.aux}, \.{.bst}, \.{.bib}, \.{.bbl},
  1321.                                                                 or \.{.blg}}
  1322. @d file_area_ilk = 8    {one of \.{texinputs:} or \.{texbib:}}
  1323. @d cite_ilk = 9         {a \.{\\citation} argument}
  1324. @d lc_cite_ilk = 10     {a \.{\\citation} argument converted to lower case}
  1325. @d bst_fn_ilk = 11      {a \.{.bst} function name}
  1326. @d bib_command_ilk = 12 {a \.{.bib}-file command}
  1327. @d macro_ilk = 13       {a \.{.bst} macro or a \.{.bib} string}
  1328. @d control_seq_ilk = 14 {a control sequence specifying a foreign character}
  1329. @d last_ilk = 14        {the same number as on the line above}
  1330. @<Types in the outer block@>=
  1331. @!hash_loc=hash_base..hash_max;         {a location within the hash table}
  1332. @!hash_pointer=empty..hash_max;         {either |empty| or a |hash_loc|}
  1333. @!str_ilk=0..last_ilk;  {the legal string types}
  1334. @<Globals in the outer block@>=
  1335. @!hash_next : packed array[hash_loc] of hash_pointer;   {coalesced-list link}
  1336. @!hash_text : packed array[hash_loc] of str_number;     {pointer to a string}
  1337. @!hash_ilk : packed array[hash_loc] of str_ilk;         {the type of string}
  1338. @!ilk_info : packed array[hash_loc] of integer;         {|ilk|-specific info}
  1339. @!hash_used : hash_base..hash_max+1;    {allocation pointer for hash table}
  1340. @!hash_found : boolean;  {set to |true| if it's already in the hash table}
  1341. @!dummy_loc : hash_loc;  {receives |str_lookup| value whenever it's useless}
  1342. @<Local variables for initialization@>=
  1343. @!k:hash_loc;
  1344. Now it's time to initialize the hash table; note that |str_start[0]|
  1345. must be unused if |hash_text[k] := 0| is to have the desired effect.
  1346. @<Set initial values of key variables@>=
  1347. for k:=hash_base to hash_max do
  1348.     begin
  1349.     hash_next[k] := empty;
  1350.     hash_text[k] := 0;  {thus, no need to initialize |hash_ilk| or |ilk_info|}
  1351.     end;
  1352. hash_used := hash_max + 1;      {nothing in table initially}
  1353. Here is the subroutine that searches the hash table for a
  1354. (string,~|str_ilk|) pair, where the string is of length |l>=0| and
  1355. appears in |buffer[j..(j+l-1)]|.  If it finds the pair, it returns the
  1356. corresponding hash-table location and sets the global variable
  1357. |hash_found| to |true|.  Otherwise it sets |hash_found| to |false|,
  1358. and if the parameter |insert_it| is |true|, it inserts the pair into
  1359. the hash table, inserts the string into |str_pool| if not previously
  1360. encountered, and returns its location.  Note that two different pairs
  1361. can have the same string but different |str_ilk|s, in which case the
  1362. second pair encountered, if |insert_it| were |true|, would be inserted
  1363. into the hash table though its string wouldn't be inserted into
  1364. |str_pool| because it would already be there.
  1365. @d max_hash_value = hash_prime+hash_prime-2+127         {|h|'s maximum value}
  1366. @d do_insert == true            {insert string if not found in hash table}
  1367. @d dont_insert == false         {don't insert string}
  1368. @d str_found = 40               {go here when you've found the string}
  1369. @d str_not_found = 45           {go here when you haven't}
  1370. @<Procedures and functions for handling numbers, characters, and strings@>=
  1371. function str_lookup(var buf:buf_type; @!j,@!l:buf_pointer; @!ilk:str_ilk;
  1372.                 @!insert_it:boolean) : hash_loc;        {search the hash table}
  1373. label str_found,@!str_not_found;
  1374. var h:0..max_hash_value;        {hash code}
  1375. @!p:hash_loc;           {index into |hash_| arrays}
  1376. @!k:buf_pointer;        {index into |buf| array}
  1377. @!old_string:boolean;   {set to |true| if it's an already encountered string}
  1378. @!str_num:str_number;   {pointer to an already encountered string}
  1379. begin
  1380. @<Compute the hash code |h|@>;
  1381. p:=h+hash_base;         {start searching here; note that |0<=h<hash_prime|}
  1382. hash_found := false;
  1383. old_string := false;
  1384.     begin
  1385.     @<Process the string if we've already encountered it@>;
  1386.     if (hash_next[p]=empty) then        {location |p| may or may not be empty}
  1387.         begin
  1388.         if (not insert_it) then goto str_not_found;
  1389.         @<Insert pair into hash table and make |p| point to it@>;
  1390.         goto str_found;
  1391.         end;
  1392.     p:=hash_next[p];            {old and new locations |p| are not empty}
  1393.     end;
  1394. str_not_found: do_nothing;      {don't insert pair; function value meaningless}
  1395. str_found: str_lookup:=p;
  1396. @^for loops@>
  1397. @.WEB@>
  1398. The value of |hash_prime| should be roughly 85\% of |hash_size|, and
  1399. it should be a prime number
  1400. (it should also be less than $2^{14} + 2^{6} = 16320$ because of
  1401. \.{WEB}'s simple-macro bound).  The theory of hashing tells us to expect
  1402. fewer than two table probes, on the average, when the search is
  1403. successful.
  1404. @<Compute the hash code |h|@>=
  1405. begin
  1406. h := 0;         {note that this works for zero-length strings}
  1407. k := j;
  1408. while (k < j+l) do      {not a |for| loop in case |j = l = 0|}
  1409.     begin
  1410.     h:=h+h+buf[k];
  1411.     while (h >= hash_prime) do h:=h-hash_prime;
  1412.     incr(k);
  1413.     end;
  1414. Here we handle the case in which we've already encountered this
  1415. string; note that even if we have, we'll still have to insert the pair
  1416. into the hash table if |str_ilk| doesn't match.
  1417. @<Process the string if we've already encountered it@>=
  1418. begin
  1419. if (hash_text[p]>0) then                        {there's something here}
  1420.     if (str_eq_buf(hash_text[p],buf,j,l)) then  {it's the right string}
  1421.         if (hash_ilk[p] = ilk) then             {it's the right |str_ilk|}
  1422.             begin
  1423.             hash_found := true;
  1424.             goto str_found;
  1425.             end
  1426.           else
  1427.             begin                               {it's the wrong |str_ilk|}
  1428.             old_string := true;
  1429.             str_num := hash_text[p];
  1430.             end;
  1431. @^for loops@>
  1432. @:BibTeX capacity exceeded}{\quad hash size@>
  1433. This code inserts the pair in the appropriate unused location.
  1434. @<Insert pair into hash table and make |p| point to it@>=
  1435. begin
  1436. if (hash_text[p]>0) then                {location |p| isn't empty}
  1437.     begin
  1438.         repeat if (hash_is_full) then overflow('hash size ',hash_size);
  1439.         decr(hash_used);
  1440.         until (hash_text[hash_used]=0); {search for an empty location}
  1441.     hash_next[p]:=hash_used;
  1442.     p:=hash_used;
  1443.     end;                        {now location |p| is empty}
  1444. if (old_string) then            {it's an already encountered string}
  1445.     hash_text[p] := str_num
  1446.   else
  1447.     begin                       {it's a new string}
  1448.     str_room(l);                {make sure it'll fit in |str_pool|}
  1449.     k := j;
  1450.     while (k < j+l) do          {not a |for| loop in case |j = l = 0|}
  1451.         begin
  1452.         append_char(buf[k]);
  1453.         incr(k);
  1454.         end;
  1455.     hash_text[p] := make_string;                {and make it official}
  1456.     end;
  1457. hash_ilk[p] := ilk;
  1458. @^string pool@>
  1459. Now that we've defined the hash-table workings we can initialize the
  1460. string pool.  Unlike \TeX, \BibTeX\ does not use a |pool_file| for
  1461. string storage; instead it inserts its pre-defined strings into
  1462. |str_pool|---this makes one file fewer for the \BibTeX\ implementor
  1463. to deal with.  This section initializes |str_pool|; the pre-defined
  1464. strings will be inserted into it shortly; and other strings are
  1465. inserted while processing the input files.
  1466. @<Set initial values of key variables@>=
  1467. pool_ptr:=0; str_ptr:=1;        {hash table must have |str_start[0]| unused}
  1468. str_start[str_ptr]:=pool_ptr;
  1469. The longest pre-defined string determines type definitions used to
  1470. insert the pre-defined strings into |str_pool|.
  1471. @d longest_pds=12       {the length of `\.{change.case\$}'}
  1472. @<Types in the outer block@>=
  1473. @!pds_loc = 1..longest_pds;
  1474. @!pds_len = 0..longest_pds;
  1475. @!pds_type = packed array [pds_loc] of char;
  1476. The variables in this program beginning with |s_| specify the
  1477. locations in |str_pool| for certain often-used strings.  Those here
  1478. have to do with the file system; the next section will actually insert
  1479. them into |str_pool|.
  1480. @<Globals in the outer block@>=
  1481. @!s_aux_extension : str_number; {\.{.aux}}
  1482. @!s_log_extension : str_number; {\.{.blg}}
  1483. @!s_bbl_extension : str_number; {\.{.bbl}}
  1484. @!s_bst_extension : str_number; {\.{.bst}}
  1485. @!s_bib_extension : str_number; {\.{.bib}}
  1486. @!s_bst_area : str_number;      {\.{texinputs:}}
  1487. @!s_bib_area : str_number;      {\.{texbib:}}
  1488. @^important note@>
  1489. @^system dependencies@>
  1490. It's time to insert some of the pre-defined strings into |str_pool|
  1491. (and thus the hash table).  These system-dependent strings should
  1492. contain no upper-case letters, and they must all be exactly
  1493. |longest_pds| characters long (even if fewer characters are actually
  1494. stored).  The |pre_define| routine appears shortly.
  1495. Important notes: These pre-definitions must not have any glitches or
  1496. the program may bomb because the |log_file| hasn't been opened yet,
  1497. and |text_ilk|s should be pre-defined later, for
  1498. \.{.bst}-function-execution purposes.
  1499. @<Pre-define certain strings@>=
  1500. pre_define('.aux        ',4,file_ext_ilk);
  1501. s_aux_extension := hash_text[pre_def_loc];
  1502. pre_define('.bbl        ',4,file_ext_ilk);
  1503. s_bbl_extension := hash_text[pre_def_loc];
  1504. pre_define('.blg        ',4,file_ext_ilk);
  1505. s_log_extension := hash_text[pre_def_loc];
  1506. pre_define('.bst        ',4,file_ext_ilk);
  1507. s_bst_extension := hash_text[pre_def_loc];
  1508. pre_define('.bib        ',4,file_ext_ilk);
  1509. s_bib_extension := hash_text[pre_def_loc];
  1510. pre_define('texinputs:  ',10,file_area_ilk);
  1511. s_bst_area := hash_text[pre_def_loc];
  1512. pre_define('texbib:     ',7,file_area_ilk);
  1513. s_bib_area := hash_text[pre_def_loc];
  1514. This global variable gives the hash-table location of pre-defined
  1515. strings generated by calls to |str_lookup|.
  1516. @<Globals in the outer block@>=
  1517. @!pre_def_loc : hash_loc;
  1518. This procedure initializes a pre-defined string of length at most
  1519. |longest_pds|.
  1520. @<Procedures and functions for handling numbers, characters, and strings@>=
  1521. procedure pre_define (@!pds:pds_type; @!len:pds_len; @!ilk:str_ilk);
  1522. var i : pds_len;
  1523. begin
  1524. for i:=1 to len do
  1525.     buffer[i] := xord[pds[i]];
  1526. pre_def_loc := str_lookup(buffer,1,len,ilk,do_insert);
  1527. These constants all begin with |n_| and are used for the |case|
  1528. statement that determines which command to execute.  The variable
  1529. |command_num| is set to one of these and is used to do the branching,
  1530. but it must have the full |integer| range because at times it can
  1531. assume an arbitrary |ilk_info| value (though it will be one of the
  1532. values here when we actually use it).
  1533. @d n_aux_bibdata = 0    {\.{\\bibdata}}
  1534. @d n_aux_bibstyle = 1   {\.{\\bibstyle}}
  1535. @d n_aux_citation = 2   {\.{\\citation}}
  1536. @d n_aux_input = 3      {\.{\\@@input}}
  1537. @d n_bst_entry = 0      {\.{entry}}
  1538. @d n_bst_execute = 1    {\.{execute}}
  1539. @d n_bst_function = 2   {\.{function}}
  1540. @d n_bst_integers = 3   {\.{integers}}
  1541. @d n_bst_iterate = 4    {\.{iterate}}
  1542. @d n_bst_macro = 5      {\.{macro}}
  1543. @d n_bst_read = 6       {\.{read}}
  1544. @d n_bst_reverse = 7    {\.{reverse}}
  1545. @d n_bst_sort = 8       {\.{sort}}
  1546. @d n_bst_strings = 9    {\.{strings}}
  1547. @d n_bib_comment = 0    {\.{comment}}
  1548. @d n_bib_preamble = 1   {\.{preamble}}
  1549. @d n_bib_string = 2     {\.{string}}
  1550. @<Globals in the outer block@>=
  1551. @!command_num : integer;
  1552. @^important note@>
  1553. Now we pre-define the command strings; they must all be exactly
  1554. |longest_pds| characters long.
  1555. Important note: These pre-definitions must not have any glitches or
  1556. the program may bomb because the |log_file| hasn't been opened yet.
  1557. @<Pre-define certain strings@>=
  1558. pre_define('\citation   ',9,aux_command_ilk);
  1559. ilk_info[pre_def_loc] := n_aux_citation;
  1560. pre_define('\bibdata    ',8,aux_command_ilk);
  1561. ilk_info[pre_def_loc] := n_aux_bibdata;
  1562. pre_define('\bibstyle   ',9,aux_command_ilk);
  1563. ilk_info[pre_def_loc] := n_aux_bibstyle;
  1564. pre_define('\@@input     ',7,aux_command_ilk);
  1565. ilk_info[pre_def_loc] := n_aux_input;
  1566. pre_define('entry       ',5,bst_command_ilk);
  1567. ilk_info[pre_def_loc] := n_bst_entry;
  1568. pre_define('execute     ',7,bst_command_ilk);
  1569. ilk_info[pre_def_loc] := n_bst_execute;
  1570. pre_define('function    ',8,bst_command_ilk);
  1571. ilk_info[pre_def_loc] := n_bst_function;
  1572. pre_define('integers    ',8,bst_command_ilk);
  1573. ilk_info[pre_def_loc] := n_bst_integers;
  1574. pre_define('iterate     ',7,bst_command_ilk);
  1575. ilk_info[pre_def_loc] := n_bst_iterate;
  1576. pre_define('macro       ',5,bst_command_ilk);
  1577. ilk_info[pre_def_loc] := n_bst_macro;
  1578. pre_define('read        ',4,bst_command_ilk);
  1579. ilk_info[pre_def_loc] := n_bst_read;
  1580. pre_define('reverse     ',7,bst_command_ilk);
  1581. ilk_info[pre_def_loc] := n_bst_reverse;
  1582. pre_define('sort        ',4,bst_command_ilk);
  1583. ilk_info[pre_def_loc] := n_bst_sort;
  1584. pre_define('strings     ',7,bst_command_ilk);
  1585. ilk_info[pre_def_loc] := n_bst_strings;
  1586. pre_define('comment     ',7,bib_command_ilk);
  1587. ilk_info[pre_def_loc] := n_bib_comment;
  1588. pre_define('preamble    ',8,bib_command_ilk);
  1589. ilk_info[pre_def_loc] := n_bib_preamble;
  1590. pre_define('string      ',6,bib_command_ilk);
  1591. ilk_info[pre_def_loc] := n_bib_string;
  1592. @* Scanning an input line.
  1593. This section describes the various |buffer| scanning routines.  The
  1594. two global variables |buf_ptr1| and |buf_ptr2| are used in scanning an
  1595. input line.  Between scans, |buf_ptr1| points to the first character
  1596. of the current token and |buf_ptr2| points to that of the next.  The
  1597. global variable |last|, set by the function |input_ln|, marks the end
  1598. of the current line; it equals 0 at the end of the current file.  All
  1599. the procedures and functions in this section will indicate an
  1600. end-of-line when it's the end of the file.
  1601. @d token_len == (buf_ptr2 - buf_ptr1)   {of the current token}
  1602. @d scan_char == buffer[buf_ptr2]        {the current character}
  1603. @<Globals in the outer block@>=
  1604. @!buf_ptr1:buf_pointer; {points to the first position of the current token}
  1605. @!buf_ptr2:buf_pointer; {used to find the end of the current token}
  1606. These macros send the current token, in |buffer[buf_ptr1]| to
  1607. |buffer[buf_ptr2-1]|, to an output file.
  1608. @d print_token == print_a_token {making this a procedure saves a little space}
  1609. @d trace_pr_token == begin
  1610.                      out_token(log_file);
  1611.                      end
  1612. @^system dependencies@>
  1613. And here are the associated procedures.  Note: The |term_out| file is
  1614. system dependent.
  1615. @<Procedures and functions for all file I/O, error messages, and such@>=
  1616. procedure out_token (var f:alpha_file);
  1617. var i:buf_pointer;
  1618. begin
  1619. i := buf_ptr1;
  1620. while (i < buf_ptr2) do
  1621.     begin
  1622.     write(f,xchr[buffer[i]]);
  1623.     incr(i);
  1624.     end;
  1625. procedure print_a_token;
  1626. begin
  1627. out_token(term_out);
  1628. out_token(log_file);
  1629. This function scans the |buffer| for the next token, starting at the
  1630. global variable |buf_ptr2| and ending just before either the single
  1631. specified stop-character or the end of the current line, whichever
  1632. comes first, respectively returning |true| or |false|; afterward,
  1633. |scan_char| is the first character following this token.
  1634. @<Procedures and functions for input scanning@>=
  1635. function scan1 (@!char1:ASCII_code) : boolean;
  1636. begin
  1637. buf_ptr1 := buf_ptr2;
  1638.                         {scan until end-of-line or the specified character}
  1639. while ((scan_char <> char1) and (buf_ptr2 < last)) do
  1640.     incr(buf_ptr2);
  1641. if (buf_ptr2 < last) then
  1642.     scan1 := true
  1643.   else
  1644.     scan1 := false;
  1645. This function is the same but stops at |white_space| characters as well.
  1646. @<Procedures and functions for input scanning@>=
  1647. function scan1_white (@!char1:ASCII_code) : boolean;
  1648. begin
  1649. buf_ptr1 := buf_ptr2;
  1650.         {scan until end-of-line, the specified character, or |white_space|}
  1651. while ((lex_class[scan_char] <> white_space) and (scan_char <> char1) and
  1652.                                                         (buf_ptr2 < last)) do
  1653.     incr(buf_ptr2);
  1654. if (buf_ptr2 < last) then
  1655.     scan1_white := true
  1656.   else
  1657.     scan1_white := false;
  1658. This function is similar to |scan1|, but stops at either of two
  1659. stop-characters as well as the end of the current line.
  1660. @<Procedures and functions for input scanning@>=
  1661. function scan2 (@!char1,@!char2:ASCII_code) : boolean;
  1662. begin
  1663. buf_ptr1 := buf_ptr2;
  1664.                         {scan until end-of-line or the specified characters}
  1665. while ((scan_char <> char1) and (scan_char <> char2) and (buf_ptr2 < last)) do
  1666.     incr(buf_ptr2);
  1667. if (buf_ptr2 < last) then
  1668.     scan2 := true
  1669.   else
  1670.     scan2 := false;
  1671. This function is the same but stops at |white_space| characters as well.
  1672. @<Procedures and functions for input scanning@>=
  1673. function scan2_white (@!char1,@!char2:ASCII_code) : boolean;
  1674. begin
  1675. buf_ptr1 := buf_ptr2;
  1676.         {scan until end-of-line, the specified characters, or |white_space|}
  1677. while ((scan_char <> char1) and (scan_char <> char2) and
  1678.                 (lex_class[scan_char] <> white_space) and (buf_ptr2 < last)) do
  1679.     incr(buf_ptr2);
  1680. if (buf_ptr2 < last) then
  1681.     scan2_white := true
  1682.   else
  1683.     scan2_white := false;
  1684. This function is similar to |scan2|, but stops at either of three
  1685. stop-characters as well as the end of the current line.
  1686. @<Procedures and functions for input scanning@>=
  1687. function scan3 (@!char1,@!char2,@!char3:ASCII_code) : boolean;
  1688. begin
  1689. buf_ptr1 := buf_ptr2;
  1690.                         {scan until end-of-line or the specified characters}
  1691. while ((scan_char <> char1) and (scan_char <> char2) and
  1692.                                 (scan_char <> char3) and (buf_ptr2 < last)) do
  1693.     incr(buf_ptr2);
  1694. if (buf_ptr2 < last) then
  1695.     scan3 := true
  1696.   else
  1697.     scan3 := false;
  1698. This function scans for letters, stopping at the first nonletter; it
  1699. returns |true| if there is at least one letter.
  1700. @<Procedures and functions for input scanning@>=
  1701. function scan_alpha : boolean;
  1702. begin
  1703. buf_ptr1 := buf_ptr2;
  1704.                                         {scan until end-of-line or a nonletter}
  1705. while ((lex_class[scan_char] = alpha) and (buf_ptr2 < last)) do
  1706.     incr(buf_ptr2);
  1707. if (token_len = 0) then
  1708.     scan_alpha := false
  1709.   else
  1710.     scan_alpha := true;
  1711. These are the possible values for |scan_result|; they're set by the
  1712. |scan_identifier| procedure and are described in the next section.
  1713. @d id_null = 0
  1714. @d specified_char_adjacent = 1
  1715. @d other_char_adjacent = 2
  1716. @d white_adjacent = 3
  1717. @<Globals in the outer block@>=
  1718. @!scan_result : id_null..white_adjacent;
  1719. This procedure scans for an identifier, stopping at the first
  1720. |illegal_id_char|, or stopping at the first character if it's
  1721. |numeric|.  It sets the global variable |scan_result| to |id_null| if
  1722. the identifier is null, else to |white_adjacent| if it ended at a
  1723. |white_space| character or an end-of-line, else to
  1724. |specified_char_adjacent| if it ended at one of |char1| or |char2| or
  1725. |char3|, else to |other_char_adjacent| if it ended at a nonspecified,
  1726. non|white_space| |illegal_id_char|.  By convention, when some calling
  1727. code really wants just one or two ``specified'' characters, it merely
  1728. repeats one of the characters.
  1729. @<Procedures and functions for input scanning@>=
  1730. procedure scan_identifier (@!char1,@!char2,@!char3:ASCII_code);
  1731. begin
  1732. buf_ptr1 := buf_ptr2;
  1733. if (lex_class[scan_char] <> numeric) then
  1734.                         {scan until end-of-line or an |illegal_id_char|}
  1735.     while ((id_class[scan_char] = legal_id_char) and (buf_ptr2 < last)) do
  1736.         incr(buf_ptr2);
  1737. if (token_len = 0) then
  1738.     scan_result := id_null
  1739. else if ((lex_class[scan_char] = white_space) or (buf_ptr2 = last)) then
  1740.     scan_result := white_adjacent
  1741. else if ((scan_char = char1) or (scan_char = char2) or (scan_char = char3))
  1742.                                                                         then
  1743.     scan_result := specified_char_adjacent
  1744.     scan_result := other_char_adjacent;
  1745. The next two procedures scan for an integer, setting the global
  1746. variable |token_value| to the corresponding integer.
  1747. @d char_value == (scan_char - "0")      {the value of the digit being scanned}
  1748. @<Globals in the outer block@>=
  1749. @!token_value : integer;        {the numeric value of the current token}
  1750. This function scans for a nonnegative integer, stopping at the first
  1751. nondigit; it sets the value of |token_value| accordingly.  It returns
  1752. |true| if the token was a legal nonnegative integer (i.e., consisted
  1753. of one or more digits).
  1754. @<Procedures and functions for input scanning@>=
  1755. function scan_nonneg_integer : boolean;
  1756. begin
  1757. buf_ptr1 := buf_ptr2;
  1758. token_value := 0;
  1759.                                         {scan until end-of-line or a nondigit}
  1760. while ((lex_class[scan_char] = numeric) and (buf_ptr2 < last)) do
  1761.     begin
  1762.     token_value := token_value*10 + char_value;
  1763.     incr(buf_ptr2);
  1764.     end;
  1765. if (token_len = 0) then                 {there were no digits}
  1766.     scan_nonneg_integer := false
  1767.   else
  1768.     scan_nonneg_integer := true;
  1769. This procedure scans for an integer, stopping at the first nondigit;
  1770. it sets the value of |token_value| accordingly.  It returns |true| if
  1771. the token was a legal integer (i.e., consisted of an optional
  1772. |minus_sign| followed by one or more digits).
  1773. @d negative == (sign_length = 1)        {if this integer is negative}
  1774. @<Procedures and functions for input scanning@>=
  1775. function scan_integer : boolean;
  1776. var sign_length : 0..1;         {1 if there's a |minus_sign|, 0 if not}
  1777. begin
  1778. buf_ptr1 := buf_ptr2;
  1779. if (scan_char = minus_sign) then        {it's a negative number}
  1780.     begin
  1781.     sign_length := 1;
  1782.     incr(buf_ptr2);                     {skip over the |minus_sign|}
  1783.     end
  1784.   else
  1785.     sign_length := 0;
  1786. token_value := 0;
  1787.                                         {scan until end-of-line or a nondigit}
  1788. while ((lex_class[scan_char] = numeric) and (buf_ptr2 < last)) do
  1789.     begin
  1790.     token_value := token_value*10 + char_value;
  1791.     incr(buf_ptr2);
  1792.     end;
  1793. if (negative) then
  1794.     token_value := -token_value;
  1795. if (token_len = sign_length) then       {there were no digits}
  1796.     scan_integer := false
  1797.   else
  1798.     scan_integer := true;
  1799. This function scans over |white_space| characters, stopping either at
  1800. the first nonwhite character or the end of the line, respectively
  1801. returning |true| or |false|.
  1802. @<Procedures and functions for input scanning@>=
  1803. function scan_white_space : boolean;
  1804. begin
  1805.                                         {scan until end-of-line or a nonwhite}
  1806. while ((lex_class[scan_char] = white_space) and (buf_ptr2 < last)) do
  1807.     incr(buf_ptr2);
  1808. if (buf_ptr2 < last) then
  1809.     scan_white_space := true
  1810.   else
  1811.     scan_white_space := false;
  1812. The |print_bad_input_line| procedure prints the current input line,
  1813. splitting it at the character being scanned: It prints |buffer[0]|,
  1814. |buffer[1]|, \dots, |buffer[buf_ptr2-1]| on one line and
  1815. |buffer[buf_ptr2]|, \dots, |buffer[last-1]| on the next (and both
  1816. lines start with a colon between two |space|s).  Each |white_space|
  1817. character is printed as a |space|.
  1818. @<Procedures and functions for all file I/O, error messages, and such@>=
  1819. procedure print_bad_input_line;
  1820. var bf_ptr : buf_pointer;
  1821. begin
  1822. print (' : ');
  1823. bf_ptr := 0;
  1824. while (bf_ptr < buf_ptr2) do
  1825.     begin
  1826.     if (lex_class[buffer[bf_ptr]] = white_space) then
  1827.         print (xchr[space])
  1828.       else
  1829.         print (xchr[buffer[bf_ptr]]);
  1830.     incr(bf_ptr);
  1831.     end;
  1832. print_newline;
  1833. print (' : ');
  1834. bf_ptr := 0;
  1835. while (bf_ptr < buf_ptr2) do
  1836.     begin
  1837.     print (xchr[space]);
  1838.     incr(bf_ptr);
  1839.     end;
  1840. bf_ptr := buf_ptr2;
  1841. while (bf_ptr < last) do
  1842.     begin
  1843.     if (lex_class[buffer[bf_ptr]] = white_space) then
  1844.         print (xchr[space])
  1845.       else
  1846.         print (xchr[buffer[bf_ptr]]);
  1847.     incr(bf_ptr);
  1848.     end;
  1849. print_newline;@/
  1850. bf_ptr := 0;
  1851. while ((bf_ptr < buf_ptr2) and (lex_class[buffer[bf_ptr]] = white_space)) do
  1852.     incr(bf_ptr);
  1853. if (bf_ptr = buf_ptr2) then
  1854.     print_ln ('(Error may have been on previous line)');
  1855. mark_error;
  1856. This little procedure exists because it's used by at least two other
  1857. procedures and thus saves some space.
  1858. @<Procedures and functions for all file I/O, error messages, and such@>=
  1859. procedure print_skipping_whatever_remains;
  1860. begin
  1861. print ('I''m skipping whatever remains of this ');
  1862. @* Getting the top-level auxiliary file name.
  1863. @^system dependencies@>
  1864. These modules read the name of the top-level \.{.aux} file.  Some
  1865. systems will try to find this on the command line; if it's not there
  1866. it will come from the user's terminal.  In either case, the name goes
  1867. into the |char| array |name_of_file|, and the files relevant to this
  1868. name are opened.
  1869. @d aux_found=41         {go here when the \.{.aux} name is legit}
  1870. @d aux_not_found=46     {go here when it's not}
  1871. @<Globals in the outer block@>=
  1872. @!aux_name_length : 0..file_name_size+1;        {\.{.aux} name sans extension}
  1873. @^system dependencies@>
  1874. @^user abuse@>
  1875. I mean, this is truly disgraceful.  A user has to type something in to
  1876. the terminal just once during the entire run.  And it's not some
  1877. complicated string where you have to get every last punctuation mark
  1878. just right, and it's not some fancy list where you get nervous because
  1879. if you forget one item you have to type the whole thing again; it's
  1880. just a simple, ordinary, file name.  Now you'd think a five-year-old
  1881. could do it; you'd think it's so simple a user should be able to do it
  1882. in his sleep.  But noooooooooo.  He had to sit there droning on and on
  1883. about who knows what until he exceeded the bounds of common sense, and
  1884. he probably didn't even realize it.  Just pitiful.  What's this world
  1885. coming to?  We should probably just delete all his files and be done
  1886. with him.  Note: The |term_out| file is system dependent.
  1887. @d sam_you_made_the_file_name_too_long == begin
  1888.                                           sam_too_long_file_name_print;
  1889.                                           goto aux_not_found;
  1890.                                           end
  1891. @<Procedures and functions for all file I/O, error messages, and such@>=
  1892. procedure sam_too_long_file_name_print;
  1893. begin
  1894. write (term_out,'File name `');
  1895. name_ptr := 1;
  1896. while (name_ptr <= aux_name_length) do
  1897.     begin
  1898.     write (term_out,name_of_file[name_ptr]);
  1899.     incr(name_ptr);
  1900.     end;
  1901. write_ln (term_out,''' is too long');
  1902. @^system dependencies@>
  1903. @^user abuse@>
  1904. We've abused the user enough for one section; suffice it to
  1905. say here that most of what we said last module still applies.
  1906. Note: The |term_out| file is system dependent.
  1907. @d sam_you_made_the_file_name_wrong == begin
  1908.                                        sam_wrong_file_name_print;
  1909.                                        goto aux_not_found;
  1910.                                        end
  1911. @<Procedures and functions for all file I/O, error messages, and such@>=
  1912. procedure sam_wrong_file_name_print;
  1913. begin
  1914. write (term_out,'I couldn''t open file name `');
  1915. name_ptr := 1;
  1916. while (name_ptr <= name_length) do
  1917.     begin
  1918.     write (term_out,name_of_file[name_ptr]);
  1919.     incr(name_ptr);
  1920.     end;
  1921. write_ln (term_out,'''');
  1922. @^system dependencies@>
  1923. This procedure consists of a loop that reads and processes a (nonnull)
  1924. \.{.aux} file name.  It's this module and the next two that must be
  1925. changed on those systems using command-line arguments.  Note: The
  1926. |term_out| and |term_in| files are system dependent.
  1927. @<Procedures and functions for the reading and processing of input files@>=
  1928. procedure get_the_top_level_aux_file_name;
  1929. label aux_found,@!aux_not_found;
  1930. var @<Variables for possible command-line processing@>@/
  1931. begin
  1932. check_cmnd_line := false;                       {many systems will change this}
  1933.     begin
  1934.     if (check_cmnd_line) then
  1935.         @<Process a possible command line@>
  1936.       else
  1937.         begin
  1938.         write (term_out,'Please type input file name (no extension)--');
  1939.         if (eoln(term_in)) then                 {so the first |read| works}
  1940.             read_ln (term_in);
  1941.         aux_name_length := 0;
  1942.         while (not eoln(term_in)) do
  1943.             begin
  1944.             if (aux_name_length = file_name_size) then
  1945.                 begin
  1946.                 while (not eoln(term_in)) do    {discard the rest of the line}
  1947.                     get(term_in);
  1948.                 sam_you_made_the_file_name_too_long;
  1949.                 end;
  1950.             incr(aux_name_length);
  1951.             name_of_file[aux_name_length] := term_in^;
  1952.             get(term_in);
  1953.             end;
  1954.         end;
  1955.     @<Handle this \.{.aux} name@>;
  1956. aux_not_found:
  1957.     check_cmnd_line := false;
  1958.     end;
  1959. aux_found:                      {now we're ready to read the \.{.aux} file}
  1960. @^system dependencies@>
  1961. The switch |check_cmnd_line| tells us whether we're to check for a
  1962. possible command-line argument.
  1963. @<Variables for possible command-line processing@>=
  1964. @!check_cmnd_line : boolean;    {|true| if we're to check the command line}
  1965. @^system dependencies@>
  1966. Here's where we do the real command-line work.  Those systems needing
  1967. more than a single module to handle the task should add the extras to
  1968. the ``System-dependent changes'' section.
  1969. @<Process a possible command line@>=
  1970. begin
  1971. do_nothing;             {the ``default system'' doesn't use the command line}
  1972. Here we orchestrate this \.{.aux} name's handling: we add the various
  1973. extensions, try to open the files with the resulting name, and
  1974. store the name strings we'll need later.
  1975. @<Handle this \.{.aux} name@>=
  1976. begin
  1977. if ((aux_name_length + length(s_aux_extension) > file_name_size) or@|
  1978.         (aux_name_length + length(s_log_extension) > file_name_size) or@|
  1979.         (aux_name_length + length(s_bbl_extension) > file_name_size)) then
  1980.     sam_you_made_the_file_name_too_long;
  1981. @<Add extensions and open files@>;
  1982. @<Put this name into the hash table@>;
  1983. goto aux_found;
  1984. Here we set up definitions and declarations for files opened in this
  1985. section.  Each element in |aux_list| (except for
  1986. |aux_list[aux_stack_size]|, which is always unused) is a pointer to
  1987. the appropriate |str_pool| string representing the \.{.aux} file name.
  1988. The array |aux_file| contains the corresponding \PASCAL\ |file|
  1989. variables.
  1990. @d cur_aux_str == aux_list[aux_ptr]  {shorthand for the current \.{.aux} file}
  1991. @d cur_aux_file == aux_file[aux_ptr]    {shorthand for the current |aux_file|}
  1992. @d cur_aux_line == aux_ln_stack[aux_ptr] {line number of current \.{.aux} file}
  1993. @<Globals in the outer block@>=
  1994. @!aux_file : array[aux_number] of alpha_file; {open \.{.aux} |file| variables}
  1995. @!aux_list : array[aux_number] of str_number;   {the open \.{.aux} file list}
  1996. @!aux_ptr : aux_number;         {points to the currently open \.{.aux} file}
  1997. @!aux_ln_stack : array[aux_number] of integer;  {open \.{.aux} line numbers}
  1998. @!top_lev_str : str_number;     {the top-level \.{.aux} file's name}
  1999. @!log_file : alpha_file;        {the |file| variable for the \.{.blg} file}
  2000. @!bbl_file : alpha_file;        {the |file| variable for the \.{.bbl} file}
  2001. Where |aux_number| is the obvious.
  2002. @<Types in the outer block@>=
  2003. @!aux_number = 0..aux_stack_size;       {gives the |aux_list| range}
  2004. @^system dependencies@>
  2005. We must make sure the (top-level) \.{.aux}, \.{.blg}, and \.{.bbl}
  2006. files can be opened.
  2007. @<Add extensions and open files@>=
  2008. begin
  2009. name_length := aux_name_length;         {set to last used position}
  2010. add_extension (s_aux_extension);        {this also sets |name_length|}
  2011. aux_ptr := 0;                           {initialize the \.{.aux} file stack}
  2012. if (not a_open_in(cur_aux_file)) then
  2013.     sam_you_made_the_file_name_wrong;
  2014. name_length := aux_name_length;
  2015. add_extension (s_log_extension);        {this also sets |name_length|}
  2016. if (not a_open_out(log_file)) then
  2017.     sam_you_made_the_file_name_wrong;
  2018. name_length := aux_name_length;
  2019. add_extension (s_bbl_extension);        {this also sets |name_length|}
  2020. if (not a_open_out(bbl_file)) then
  2021.     sam_you_made_the_file_name_wrong;
  2022. @:this can't happen}{\quad Already encountered auxiliary file@>
  2023. This code puts the \.{.aux} file name, both with and without the
  2024. extension, into the hash table, and it initializes |aux_list|.  Note
  2025. that all previous top-level \.{.aux}-file stuff must have been
  2026. successful.
  2027. @<Put this name into the hash table@>=
  2028. begin
  2029. name_length := aux_name_length;
  2030. add_extension (s_aux_extension);        {this also sets |name_length|}
  2031. name_ptr := 1;
  2032. while (name_ptr <= name_length) do
  2033.     begin
  2034.     buffer[name_ptr] := xord[name_of_file[name_ptr]];
  2035.     incr(name_ptr);
  2036.     end;
  2037. top_lev_str := hash_text[
  2038.                 str_lookup(buffer,1,aux_name_length,text_ilk,do_insert)];
  2039. cur_aux_str := hash_text[
  2040.                 str_lookup(buffer,1,name_length,aux_file_ilk,do_insert)];
  2041.                                 {note that this has initialized |aux_list|}
  2042. if (hash_found) then
  2043.     begin
  2044.       trace
  2045.       print_aux_name;
  2046.       ecart@/
  2047.     confusion ('Already encountered auxiliary file');
  2048.     end;
  2049. cur_aux_line := 0;   {this finishes initializing the top-level \.{.aux} file}
  2050. Print the name of the current \.{.aux} file, followed by a |newline|.
  2051. @<Procedures and functions for all file I/O, error messages, and such@>=
  2052. procedure print_aux_name;
  2053. begin
  2054. print_pool_str (cur_aux_str);
  2055. print_newline;
  2056. @* Reading the auxiliary file(s).
  2057. @^auxiliary-file commands@>
  2058. Now it's time to read the \.{.aux} file.  The only commands we handle
  2059. are \.{\\citation} (there can be arbitrarily many, each having
  2060. arbitrarily many arguments), \.{\\bibdata} (there can be just one, but
  2061. it can have arbitrarily many arguments), \.{\\bibstyle} (there can be
  2062. just one, and it can have just one argument), and \.{\\@@input} (there
  2063. can be arbitrarily many, each with one argument, and they can be
  2064. nested to a depth of |aux_stack_size|).  Each of these commands is
  2065. assumed to be on just a single line.  The rest of the \.{.aux} file is
  2066. ignored.
  2067. @d aux_done=31          {go here when finished with the \.{.aux} files}
  2068. @<Labels in the outer block@>=
  2069. ,@!aux_done
  2070. We keep reading and processing input lines until none left.  This is
  2071. part of the main program; hence, because of the |aux_done| label,
  2072. there's no conventional |begin|-|end| pair surrounding the entire
  2073. module.
  2074. @<Read the \.{.aux} file@>=
  2075. print ('The top-level auxiliary file: ');
  2076. print_aux_name;
  2077.     begin                       {|pop_the_aux_stack| will exit the loop}
  2078.     incr(cur_aux_line);
  2079.     if (not input_ln(cur_aux_file)) then        {end of current \.{.aux} file}
  2080.         pop_the_aux_stack
  2081.       else
  2082.         get_aux_command_and_process;
  2083.     end;
  2084.   trace
  2085.   trace_pr_ln ('Finished reading the auxiliary file(s)');
  2086.   ecart@/
  2087. aux_done:
  2088. last_check_for_aux_errors;
  2089. When we find a bug, we print a message and flush the rest of the line.
  2090. This macro must be called from within a procedure that has an |exit|
  2091. label.
  2092. @d aux_err_return == begin
  2093.                      aux_err_print;
  2094.                      return;            {flush this input line}
  2095.                      end
  2096. @d aux_err(#) == begin
  2097.                  print (#);
  2098.                  aux_err_return;
  2099.                  end
  2100. @<Procedures and functions for all file I/O, error messages, and such@>=
  2101. procedure aux_err_print;
  2102. begin
  2103. print ('---line ',cur_aux_line:0,' of file ');
  2104. print_aux_name;@/
  2105. print_bad_input_line;                   {this call does the |mark_error|}
  2106. print_skipping_whatever_remains;
  2107. print_ln ('command')
  2108. @:this can't happen}{\quad Illegal auxiliary-file command@>
  2109. Here are a bunch of macros whose print statements are used at least
  2110. twice.  Thus we save space by making the statements procedures.  This
  2111. macro complains when there's a repeated command that's to be used just
  2112. once.
  2113. @d aux_err_illegal_another(#) == begin
  2114.                                  aux_err_illegal_another_print (#);
  2115.                                  aux_err_return;
  2116.                                  end
  2117. @<Procedures and functions for all file I/O, error messages, and such@>=
  2118. procedure aux_err_illegal_another_print (@!cmd_num : integer);
  2119. begin
  2120. print ('Illegal, another \bib');
  2121. case (cmd_num) of
  2122.     n_aux_bibdata : print ('data');
  2123.     n_aux_bibstyle : print ('style');
  2124.     othercases
  2125.         confusion ('Illegal auxiliary-file command')
  2126. endcases;
  2127. print (' command');
  2128. This one complains when a command is missing its |right_brace|.
  2129. @d aux_err_no_right_brace == begin
  2130.                              aux_err_no_right_brace_print;
  2131.                              aux_err_return;
  2132.                              end
  2133. @<Procedures and functions for all file I/O, error messages, and such@>=
  2134. procedure aux_err_no_right_brace_print;
  2135. begin
  2136. print ('No "',xchr[right_brace],'"');
  2137. This one complains when a command has stuff after its |right_brace|.
  2138. @d aux_err_stuff_after_right_brace == begin
  2139.                                       aux_err_stuff_after_right_brace_print;
  2140.                                       aux_err_return;
  2141.                                       end
  2142. @<Procedures and functions for all file I/O, error messages, and such@>=
  2143. procedure aux_err_stuff_after_right_brace_print;
  2144. begin
  2145. print ('Stuff after "',xchr[right_brace],'"');
  2146. And this one complains when a command has |white_space| in its
  2147. argument.
  2148. @d aux_err_white_space_in_argument == begin
  2149.                                       aux_err_white_space_in_argument_print;
  2150.                                       aux_err_return;
  2151.                                       end
  2152. @<Procedures and functions for all file I/O, error messages, and such@>=
  2153. procedure aux_err_white_space_in_argument_print;
  2154. begin
  2155. print ('White space in argument');
  2156. @^auxiliary-file commands@>
  2157. @:this can't happen}{\quad Unknown auxiliary-file command@>
  2158. We're not at the end of an \.{.aux} file, so we see if the current
  2159. line might be a command of interest.  A command of interest will be a
  2160. line without blanks, consisting of a command name, a |left_brace|, one
  2161. or more arguments separated by commas, and a |right_brace|.
  2162. @<Scan for and process an \.{.aux} command@>=
  2163. procedure get_aux_command_and_process;
  2164. label exit;
  2165. begin
  2166. buf_ptr2 := 0;                          {mark the beginning of the next token}
  2167. if (not scan1(left_brace)) then         {no |left_brace|---flush line}
  2168.     return;
  2169. command_num := ilk_info[
  2170.         str_lookup(buffer,buf_ptr1,token_len,aux_command_ilk,dont_insert)];
  2171. if (hash_found) then
  2172.     case (command_num) of
  2173.         n_aux_bibdata : aux_bib_data_command;
  2174.         n_aux_bibstyle : aux_bib_style_command;
  2175.         n_aux_citation : aux_citation_command;
  2176.         n_aux_input : aux_input_command;
  2177.         othercases
  2178.             confusion ('Unknown auxiliary-file command')
  2179.     endcases;
  2180. exit:
  2181. Here we introduce some variables for processing a \.{\\bibdata}
  2182. command.  Each element in |bib_list| (except for
  2183. |bib_list[max_bib_files]|, which is always unused) is a pointer to the
  2184. appropriate |str_pool| string representing the \.{.bib} file name.
  2185. The array |bib_file| contains the corresponding \PASCAL\ |file|
  2186. variables.
  2187. @d cur_bib_str == bib_list[bib_ptr]     {shorthand for current \.{.bib} file}
  2188. @d cur_bib_file == bib_file[bib_ptr]    {shorthand for current |bib_file|}
  2189. @<Globals in the outer block@>=
  2190. @!bib_list : array[bib_number] of str_number;   {the \.{.bib} file list}
  2191. @!bib_ptr : bib_number;         {pointer for the current \.{.bib} file}
  2192. @!num_bib_files : bib_number;   {the total number of \.{.bib} files}
  2193. @!bib_seen : boolean;   {|true| if we've already seen a \.{\\bibdata} command}
  2194. @!bib_file : array[bib_number] of alpha_file; {corresponding |file| variables}
  2195. Where |bib_number| is the obvious.
  2196. @<Types in the outer block@>=
  2197. @!bib_number = 0..max_bib_files;        {gives the |bib_list| range}
  2198. @<Set initial values of key variables@>=
  2199. bib_ptr := 0;           {this makes |bib_list| empty}
  2200. bib_seen := false;      {we haven't seen a \.{\\bibdata} command yet}
  2201. @:auxiliary-file commands}{\quad \.{\\bibdata}@>
  2202. A \.{\\bibdata} command will have its arguments between braces and
  2203. separated by commas.  There must be exactly one such command in the
  2204. \.{.aux} file(s).  All upper-case letters are converted to lower case.
  2205. @<Procedures and functions for the reading and processing of input files@>=
  2206. procedure aux_bib_data_command;
  2207. label exit;
  2208. begin
  2209. if (bib_seen) then
  2210.     aux_err_illegal_another (n_aux_bibdata);
  2211. bib_seen := true;       {now we've seen a \.{\\bibdata} command}
  2212. while (scan_char <> right_brace) do
  2213.     begin
  2214.     incr(buf_ptr2);                     {skip over the previous stop-character}
  2215.     if (not scan2_white(right_brace,comma)) then
  2216.         aux_err_no_right_brace;
  2217.     if (lex_class[scan_char] = white_space) then
  2218.         aux_err_white_space_in_argument;
  2219.     if ((last > buf_ptr2+1) and (scan_char = right_brace)) then
  2220.         aux_err_stuff_after_right_brace;
  2221.     @<Open a \.{.bib} file@>;
  2222.     end;
  2223. exit:
  2224. Here's a procedure we'll need shortly.  It prints the name of the
  2225. current \.{.bib} file, followed by a |newline|.
  2226. @<Procedures and functions for all file I/O, error messages, and such@>=
  2227. procedure print_bib_name;
  2228. begin
  2229. print_pool_str (cur_bib_str);
  2230. print_pool_str (s_bib_extension);
  2231. print_newline;
  2232. This macro is similar to |aux_err| but it complains specifically about
  2233. opening a file for a \.{\\bibdata} command.
  2234. @d open_bibdata_aux_err(#) == begin
  2235.                               print (#);
  2236.                               print_bib_name;
  2237.                               aux_err_return;   {this does the |mark_error|}
  2238.                               end
  2239. @:BibTeX capacity exceeded}{\quad number of \.{.bib} files@>
  2240. Now we add the just-found argument to |bib_list| if it hasn't already
  2241. been encountered as a \.{\\bibdata} argument and if, after appending
  2242. the |s_bib_extension| string, the resulting file name can be opened.
  2243. @<Open a \.{.bib} file@>=
  2244. begin
  2245. if (bib_ptr = max_bib_files) then
  2246.     overflow('number of database files ',max_bib_files);
  2247. cur_bib_str := hash_text[
  2248.                 str_lookup(buffer,buf_ptr1,token_len,bib_file_ilk,do_insert)];
  2249. if (hash_found) then    {already encountered this as a \.{\\bibdata} argument}
  2250.     open_bibdata_aux_err ('This database file appears more than once: ');
  2251. start_name (cur_bib_str);
  2252. add_extension (s_bib_extension);
  2253. if (not a_open_in(cur_bib_file)) then
  2254.     begin
  2255.     add_area (s_bib_area);
  2256.     if (not a_open_in(cur_bib_file)) then
  2257.         open_bibdata_aux_err ('I couldn''t open database file ');
  2258.     end;
  2259.   trace
  2260.   trace_pr_pool_str (cur_bib_str);
  2261.   trace_pr_pool_str (s_bib_extension);
  2262.   trace_pr_ln (' is a bibdata file');
  2263.   ecart@/
  2264. incr(bib_ptr);
  2265. Here we introduce some variables for processing a \.{\\bibstyle}
  2266. command.
  2267. @<Globals in the outer block@>=
  2268. @!bst_seen : boolean;   {|true| if we've already seen a \.{\\bibstyle} command}
  2269. @!bst_str : str_number;         {the string number for the \.{.bst} file}
  2270. @!bst_file : alpha_file;        {the corresponding |file| variable}
  2271. And we initialize.
  2272. @<Set initial values of key variables@>=
  2273. bst_str := 0;           {mark |bst_str| as unused}
  2274. bst_seen := false;      {we haven't seen a \.{\\bibstyle} command yet}
  2275. @:auxiliary-file commands}{\quad \.{\\bibstyle}@>
  2276. A \.{\\bibstyle} command will have exactly one argument, and it will
  2277. be between braces.  There must be exactly one such command in the
  2278. \.{.aux} file(s).  All upper-case letters are converted to lower case.
  2279. @<Procedures and functions for the reading and processing of input files@>=
  2280. procedure aux_bib_style_command;
  2281. label exit;
  2282. begin
  2283. if (bst_seen) then
  2284.     aux_err_illegal_another (n_aux_bibstyle);
  2285. bst_seen := true;               {now we've seen a \.{\\bibstyle} command}
  2286. incr(buf_ptr2);                 {skip over the |left_brace|}
  2287. if (not scan1_white(right_brace)) then
  2288.     aux_err_no_right_brace;
  2289. if (lex_class[scan_char] = white_space) then
  2290.     aux_err_white_space_in_argument;
  2291. if (last > buf_ptr2+1) then
  2292.     aux_err_stuff_after_right_brace;
  2293. @<Open the \.{.bst} file@>;
  2294. exit:
  2295. @:this can't happen}{\quad Already encountered style file@>
  2296. Now we open the file whose name is the just-found argument appended
  2297. with the |s_bst_extension| string, if possible.
  2298. @<Open the \.{.bst} file@>=
  2299. begin
  2300. bst_str := hash_text[
  2301.                 str_lookup(buffer,buf_ptr1,token_len,bst_file_ilk,do_insert)];
  2302. if (hash_found) then
  2303.     begin
  2304.       trace
  2305.       print_bst_name;
  2306.       ecart@/
  2307.     confusion ('Already encountered style file');
  2308.     end;
  2309. start_name (bst_str);
  2310. add_extension (s_bst_extension);
  2311. if (not a_open_in(bst_file)) then
  2312.     begin
  2313.     add_area (s_bst_area);
  2314.     if (not a_open_in(bst_file)) then
  2315.         begin
  2316.         print ('I couldn''t open style file ');
  2317.         print_bst_name;@/
  2318.         bst_str := 0;                           {mark as unused again}
  2319.         aux_err_return;
  2320.         end;
  2321.     end;
  2322. print ('The style file: ');
  2323. print_bst_name;
  2324. Print the name of the \.{.bst} file, followed by a |newline|.
  2325. @<Procedures and functions for all file I/O, error messages, and such@>=
  2326. procedure print_bst_name;
  2327. begin
  2328. print_pool_str (bst_str);
  2329. print_pool_str (s_bst_extension);
  2330. print_newline;
  2331. Here we introduce some variables for processing a \.{\\citation}
  2332. command.  Each element in |cite_list| (except for
  2333. |cite_list[max_cites]|, which is always unused) is a pointer to the
  2334. appropriate |str_pool| string.  The cite-key list is kept in order of
  2335. occurrence with duplicates removed.
  2336. @d cur_cite_str == cite_list[cite_ptr]  {shorthand for the current cite key}
  2337. @<Globals in the outer block@>=
  2338. @!cite_list : packed array[cite_number] of str_number;  {the cite-key list}
  2339. @!cite_ptr : cite_number;       {pointer for the current cite key}
  2340. @!entry_cite_ptr : cite_number; {cite pointer for the current entry}
  2341. @!num_cites : cite_number;      {the total number of distinct cite keys}
  2342. @!old_num_cites : cite_number;  {set to a previous |num_cites| value}
  2343. @!citation_seen : boolean;      {|true| if we've seen a \.{\\citation} command}
  2344. @!cite_loc : hash_loc;          {the hash-table location of a cite key}
  2345. @!lc_cite_loc : hash_loc;       {and of its lower-case equivalent}
  2346. @!lc_xcite_loc : hash_loc;      {a second |lc_cite_loc| variable}
  2347. @!cite_found : boolean;         {|true| if we've already seen this cite key}
  2348. @!all_entries : boolean;        {|true| if we're to use the entire database}
  2349. @!all_marker : cite_number;     {we put the other entries in |cite_list| here}
  2350. Where |cite_number| is the obvious.
  2351. @<Types in the outer block@>=
  2352. @!cite_number = 0..max_cites;   {gives the |cite_list| range}
  2353. @<Set initial values of key variables@>=
  2354. cite_ptr := 0;          {this makes |cite_list| empty}
  2355. citation_seen := false; {we haven't seen a \.{\\citation} command yet}
  2356. all_entries := false;   {by default, use just the entries explicitly named}
  2357. @^case mismatch@>
  2358. @^entire database inclusion@>
  2359. @^whole database inclusion@>
  2360. @:LaTeX}{\LaTeX@>
  2361. @:auxiliary-file commands}{\quad \.{\\citation}@>
  2362. A \.{\\citation} command will have its arguments between braces and
  2363. separated by commas.  Upper/lower cases are considered to be different
  2364. for \.{\\citation} arguments, which is the same as the rest of \LaTeX\
  2365. but different from the rest of \BibTeX.  A cite key needn't exactly
  2366. case-match its corresponding database key to work, although two cite
  2367. keys that are case-mismatched will produce an error message.
  2368. (A {\sl case mismatch\/} is a mismatch, but only because of a case
  2369. difference.)
  2370. A \.{\\citation} command having \.{*} as an argument indicates that
  2371. the entire database will be included (almost as if a \.{\\nocite}
  2372. command that listed every cite key in the database, in order, had been
  2373. given at the corresponding spot in the \.{.tex} file).
  2374. @d next_cite = 23       {read the next argument}
  2375. @<Procedures and functions for the reading and processing of input files@>=
  2376. procedure aux_citation_command;
  2377. label next_cite,@!exit;
  2378. begin
  2379. citation_seen := true;          {now we've seen a \.{\\citation} command}
  2380. while (scan_char <> right_brace) do
  2381.     begin
  2382.     incr(buf_ptr2);             {skip over the previous stop-character}
  2383.     if (not scan2_white(right_brace,comma)) then
  2384.         aux_err_no_right_brace;
  2385.     if (lex_class[scan_char] = white_space) then
  2386.         aux_err_white_space_in_argument;
  2387.     if ((last > buf_ptr2+1) and (scan_char = right_brace)) then
  2388.         aux_err_stuff_after_right_brace;
  2389.     @<Check the cite key@>;
  2390. next_cite:
  2391.     end;
  2392. exit:
  2393. @^kludge@>
  2394. We must check if (the lower-case version of) this cite key has been
  2395. previously encountered, and proceed accordingly.  The alias kludge
  2396. helps make the stack space not overflow on some machines.
  2397. @d ex_buf1== ex_buf             {an alias, used only in this module}
  2398. @<Check the cite key@>=
  2399. begin
  2400.   trace
  2401.   trace_pr_token;
  2402.   trace_pr (' cite key encountered');
  2403.   ecart@/
  2404. @<Check for entire database inclusion (and thus skip this cite key)@>;
  2405. tmp_ptr := buf_ptr1;
  2406. while (tmp_ptr < buf_ptr2) do
  2407.     begin
  2408.     ex_buf1[tmp_ptr] := buffer[tmp_ptr];
  2409.     incr(tmp_ptr);
  2410.     end;
  2411. lower_case (ex_buf1, buf_ptr1, token_len);      {convert to `canonical' form}
  2412. lc_cite_loc := str_lookup(ex_buf1,buf_ptr1,token_len,lc_cite_ilk,do_insert);
  2413. if (hash_found) then    {already encountered this as a \.{\\citation} argument}
  2414.     @<Cite seen, don't add a cite key@>
  2415.   else
  2416.     @<Cite unseen, add a cite key@>;
  2417.                                 {it's a new cite key---add it to |cite_list|}
  2418. Here we check for a \.{\\citation} command having \.{*} as an
  2419. argument, indicating that the entire database will be included.
  2420. @<Check for entire database inclusion (and thus skip this cite key)@>=
  2421. begin
  2422. if (token_len = 1) then
  2423.   if (buffer[buf_ptr1] = star) then
  2424.     begin
  2425.       trace
  2426.       trace_pr_ln ('---entire database to be included');
  2427.       ecart@/
  2428.     if (all_entries) then
  2429.         begin
  2430.         print_ln ('Multiple inclusions of entire database');
  2431.         aux_err_return;
  2432.         end
  2433.       else
  2434.         begin
  2435.         all_entries := true;
  2436.         all_marker := cite_ptr;
  2437.         goto next_cite;
  2438.         end;
  2439.     end;
  2440. @^case mismatch errors@>
  2441. We've previously encountered the lower-case version, so we check that
  2442. the actual version exactly matches the actual version of the
  2443. previously-encountered cite key(s).
  2444. @<Cite seen, don't add a cite key@>=
  2445. begin
  2446.   trace
  2447.   trace_pr_ln (' previously');
  2448.   ecart@/
  2449. dummy_loc := str_lookup(buffer,buf_ptr1,token_len,cite_ilk,dont_insert);
  2450. if (not hash_found) then                {case mismatch error}
  2451.     begin
  2452.     print ('Case mismatch error between cite keys ');
  2453.     print_token;
  2454.     print (' and ');
  2455.     print_pool_str (cite_list[ilk_info[ilk_info[lc_cite_loc]]]);
  2456.     print_newline;
  2457.     aux_err_return;
  2458.     end;
  2459. @:this can't happen}{\quad Cite hash error@>
  2460. Now we add the just-found argument to |cite_list| if there isn't
  2461. anything funny happening.
  2462. @<Cite unseen, add a cite key@>=
  2463. begin
  2464.   trace
  2465.   trace_pr_newline;
  2466.   ecart@/
  2467. cite_loc := str_lookup(buffer,buf_ptr1,token_len,cite_ilk,do_insert);
  2468. if (hash_found) then
  2469.     hash_cite_confusion;
  2470. check_cite_overflow (cite_ptr);
  2471. cur_cite_str := hash_text[cite_loc];
  2472. ilk_info[cite_loc] := cite_ptr;
  2473. ilk_info[lc_cite_loc] := cite_loc;
  2474. incr(cite_ptr);
  2475. @:this can't happen}{\quad Cite hash error@>
  2476. Here's a serious complaint (that is, a bug) concerning hash problems.
  2477. This is the first of several similar bug-procedures that exist only
  2478. because they save space.
  2479. @<Procedures and functions for all file I/O, error messages, and such@>=
  2480. procedure hash_cite_confusion;
  2481. begin
  2482. confusion ('Cite hash error');
  2483. @^fetish@>
  2484. @:BibTeX capacity exceeded}{\quad number of cite keys@>
  2485. Complain if somebody's got a cite fetish.  This procedure is called
  2486. when were about to add another cite key to |cite_list|.  It assumes
  2487. that |cite_loc| gives the potential cite key's hash table location.
  2488. @<Procedures and functions for all file I/O, error messages, and such@>=
  2489. procedure check_cite_overflow (@!last_cite : cite_number);
  2490. begin
  2491. if (last_cite = max_cites) then
  2492.     begin
  2493.     print_pool_str (hash_text[cite_loc]);
  2494.     print_ln (' is the key:');
  2495.     overflow('number of cite keys ',max_cites);
  2496.     end;
  2497. @:auxiliary-file commands}{\quad \.{\\\AT!input}@>
  2498. An \.{\\@@input} command will have exactly one argument, it will
  2499. be between braces, and it must have the |s_aux_extension|.
  2500. All upper-case letters are converted to lower case.
  2501. @<Procedures and functions for the reading and processing of input files@>=
  2502. procedure aux_input_command;
  2503. label exit;
  2504. var aux_extension_ok : boolean;         {to check for a correct file extension}
  2505. begin
  2506. incr(buf_ptr2);                         {skip over the |left_brace|}
  2507. if (not scan1_white(right_brace)) then
  2508.     aux_err_no_right_brace;
  2509. if (lex_class[scan_char] = white_space) then
  2510.     aux_err_white_space_in_argument;
  2511. if (last > buf_ptr2+1) then
  2512.     aux_err_stuff_after_right_brace;
  2513. @<Push the \.{.aux} stack@>;
  2514. exit:
  2515. @:BibTeX capacity exceeded}{\quad number of \.{.aux} files@>
  2516. We must check that this potential \.{.aux} file won't overflow the
  2517. stack, that it has the correct extension, that we haven't encountered
  2518. it before (to prevent, among other things, an infinite loop).
  2519. @<Push the \.{.aux} stack@>=
  2520. begin
  2521. incr(aux_ptr);
  2522. if (aux_ptr = aux_stack_size) then
  2523.     begin
  2524.     print_token; print (': ');
  2525.     overflow('auxiliary file depth ',aux_stack_size);
  2526.     end;
  2527. aux_extension_ok := true;
  2528. if (token_len < length(s_aux_extension)) then@/
  2529.     aux_extension_ok := false   {else |str_eq_buf| might bomb the program}
  2530. else if (not str_eq_buf(s_aux_extension, buffer,
  2531.         buf_ptr2-length(s_aux_extension), length(s_aux_extension))) then
  2532.     aux_extension_ok := false;
  2533. if (not aux_extension_ok) then
  2534.     begin
  2535.     print_token;
  2536.     print (' has a wrong extension');
  2537.     decr(aux_ptr);
  2538.     aux_err_return;
  2539.     end;
  2540. cur_aux_str := hash_text[
  2541.                 str_lookup(buffer,buf_ptr1,token_len,aux_file_ilk,do_insert)];
  2542. if (hash_found) then
  2543.     begin
  2544.     print ('Already encountered file ');
  2545.     print_aux_name;
  2546.     decr(aux_ptr);
  2547.     aux_err_return;
  2548.     end;
  2549. @<Open this \.{.aux} file@>;
  2550. We check that this \.{.aux} file can actually be opened, and then open it.
  2551. @<Open this \.{.aux} file@>=
  2552. begin
  2553. start_name (cur_aux_str);       {extension already there for \.{.aux} files}
  2554. name_ptr := name_length+1;
  2555. while (name_ptr <= file_name_size) do   {pad with blanks}
  2556.     begin
  2557.     name_of_file[name_ptr] := ' ';
  2558.     incr(name_ptr);
  2559.     end;
  2560. if (not a_open_in(cur_aux_file)) then
  2561.     begin
  2562.     print ('I couldn''t open auxiliary file ');
  2563.     print_aux_name;
  2564.     decr(aux_ptr);
  2565.     aux_err_return;
  2566.     end;
  2567. print ('A level-',aux_ptr:0,' auxiliary file: ');
  2568. print_aux_name;
  2569. cur_aux_line := 0;
  2570. Here we close the current-level \.{.aux} file and go back up a level,
  2571. if possible, by decrementing |aux_ptr|.
  2572. @<Procedures and functions for the reading and processing of input files@>=
  2573. procedure pop_the_aux_stack;
  2574. begin
  2575. a_close (cur_aux_file);
  2576. if (aux_ptr=0) then
  2577.     goto aux_done
  2578.   else
  2579.     decr(aux_ptr);
  2580. @^gymnastics@>
  2581. That's it for processing \.{.aux} commands, except for finishing the
  2582. procedural gymnastics.
  2583. @<Procedures and functions for the reading and processing of input files@>=
  2584. @<Scan for and process an \.{.aux} command@>
  2585. We must complain if anything's amiss.
  2586. @d aux_end_err(#) == begin
  2587.                      aux_end1_err_print;
  2588.                      print (#);
  2589.                      aux_end2_err_print;
  2590.                      end
  2591. @<Procedures and functions for all file I/O, error messages, and such@>=
  2592. procedure aux_end1_err_print;
  2593. begin
  2594. print ('I found no ');
  2595. procedure aux_end2_err_print;
  2596. begin
  2597. print ('---while reading file ');
  2598. print_aux_name;
  2599. mark_error;
  2600. Before proceeding, we see if we have any complaints.
  2601. @<Procedures and functions for the reading and processing of input files@>=
  2602. procedure last_check_for_aux_errors;
  2603. begin
  2604. num_cites := cite_ptr;          {record the number of distinct cite keys}
  2605. num_bib_files := bib_ptr;       {and the number of \.{.bib} files}
  2606. if (not citation_seen) then
  2607.     aux_end_err ('\citation commands')
  2608.   else if ((num_cites = 0) and (not all_entries)) then
  2609.     aux_end_err ('cite keys');
  2610. if (not bib_seen) then
  2611.     aux_end_err ('\bibdata command')
  2612.   else if (num_bib_files = 0) then
  2613.     aux_end_err ('database files');
  2614. if (not bst_seen) then
  2615.     aux_end_err ('\bibstyle command')
  2616.   else if (bst_str = 0) then
  2617.     aux_end_err ('style file');
  2618. @* Reading the style file.
  2619. This part of the program reads the \.{.bst} file, which consists of a
  2620. sequence of commands.  Each \.{.bst} command consists of a name (for
  2621. which case differences are ignored) followed by zero or more
  2622. arguments, each enclosed in braces.
  2623. @d bst_done=32          {go here when finished with the \.{.bst} file}
  2624. @d no_bst_file=9932     {go here when skipping the \.{.bst} file}
  2625. @<Labels in the outer block@>=
  2626. ,@!bst_done,@!no_bst_file
  2627. The |bbl_line_num| gets initialized along with the |bst_line_num|, so
  2628. it's declared here too.
  2629. @<Globals in the outer block@>=
  2630. @!bbl_line_num : integer;       {line number of the \.{.bbl} (output) file}
  2631. @!bst_line_num : integer;       {line number of the \.{.bst} file}
  2632. This little procedure exists because it's used by at least two other
  2633. procedures and thus saves some space.
  2634. @<Procedures and functions for all file I/O, error messages, and such@>=
  2635. procedure bst_ln_num_print;
  2636. begin
  2637. print ('--line ',bst_line_num:0,' of file ');
  2638. print_bst_name;
  2639. When there's a serious error parsing the \.{.bst} file, we flush the
  2640. rest of the current command; a blank line is assumed to mark the end
  2641. of a command (but for the purposes of error recovery only).  Thus,
  2642. error recovery will be better if style designers leave blank lines
  2643. between \.{.bst} commands.  This macro must be called from within a
  2644. procedure that has an |exit| label.
  2645. @d bst_err_print_and_look_for_blank_line_return ==
  2646.                 begin
  2647.                 bst_err_print_and_look_for_blank_line;
  2648.                 return;
  2649.                 end
  2650. @d bst_err(#) == begin          {serious error during \.{.bst} parsing}
  2651.                  print (#);
  2652.                  bst_err_print_and_look_for_blank_line_return;
  2653.                  end
  2654. @<Procedures and functions for all file I/O, error messages, and such@>=
  2655. procedure bst_err_print_and_look_for_blank_line;
  2656. begin
  2657. print ('-');
  2658. bst_ln_num_print;
  2659. print_bad_input_line;                   {this call does the |mark_error|}
  2660. while (last <> 0) do                    {look for a blank input line}
  2661.     if (not input_ln(bst_file)) then    {or the end of the file}
  2662.         goto bst_done
  2663.       else
  2664.         incr(bst_line_num);
  2665. buf_ptr2 := last;                       {to input the next line}
  2666. When there's a harmless error parsing the \.{.bst} file (harmless
  2667. syntactically, at least) we give just a |warning_message|.
  2668. @d bst_warn(#) == begin         {non-serious error during \.{.bst} parsing}
  2669.                   print (#);
  2670.                   bst_warn_print;
  2671.                   end
  2672. @<Procedures and functions for all file I/O, error messages, and such@>=
  2673. procedure bst_warn_print;
  2674. begin
  2675. bst_ln_num_print;
  2676. mark_warning;
  2677. Here's the outer loop for reading the \.{.bst} file---it keeps reading
  2678. and processing \.{.bst} commands until none left.  This is part of the
  2679. main program; hence, because of the |bst_done| label, there's no
  2680. conventional |begin|-|end| pair surrounding the entire module.
  2681. @<Read and execute the \.{.bst} file@>=
  2682. if (bst_str = 0) then   {there's no \.{.bst} file to read}
  2683.     goto no_bst_file;   {this is a |goto| so that |bst_done| is not in a block}
  2684. bst_line_num := 0;      {initialize things}
  2685. bbl_line_num := 1;      {best spot to initialize the output line number}
  2686. buf_ptr2 := last;       {to get the first input line}
  2687.     begin
  2688.     if (not eat_bst_white_space) then   {the end of the \.{.bst} file}
  2689.         goto bst_done;
  2690.     get_bst_command_and_process;
  2691.     end;
  2692. bst_done: a_close (bst_file);
  2693. no_bst_file: a_close (bbl_file);
  2694. This \.{.bst}-specific scanning function skips over |white_space|
  2695. characters (and comments) until hitting a nonwhite character or the
  2696. end of the file, respectively returning |true| or |false|.  It also
  2697. updates |bst_line_num|, the line counter.
  2698. @<Procedures and functions for input scanning@>=
  2699. function eat_bst_white_space : boolean;
  2700. label exit;
  2701. begin
  2702.     begin
  2703.     if (scan_white_space) then          {hit a nonwhite character on this line}
  2704.         if (scan_char <> comment) then  {it's not a comment character; return}
  2705.             begin
  2706.             eat_bst_white_space := true;
  2707.             return;
  2708.             end;
  2709.     if (not input_ln(bst_file)) then    {end-of-file; return |false|}
  2710.         begin
  2711.         eat_bst_white_space := false;
  2712.         return;
  2713.         end;
  2714.     incr(bst_line_num);
  2715.     buf_ptr2 := 0;
  2716.     end;
  2717. exit:
  2718. It's often illegal to end a \.{.bst} command in certain places, and
  2719. this is where we come to check.
  2720. @d eat_bst_white_and_eof_check(#) ==
  2721.         begin
  2722.         if (not eat_bst_white_space) then
  2723.             begin
  2724.             eat_bst_print;
  2725.             bst_err (#);
  2726.             end;
  2727.         end
  2728. @<Procedures and functions for all file I/O, error messages, and such@>=
  2729. procedure eat_bst_print;
  2730. begin
  2731. print ('Illegal end of style file in command: ');
  2732. We must attend to a few details before getting to work on this
  2733. \.{.bst} command.
  2734. @<Scan for and process a \.{.bst} command@>=
  2735. procedure get_bst_command_and_process;
  2736. label exit;
  2737. begin
  2738. if (not scan_alpha) then
  2739.     bst_err ('"',xchr[scan_char],'" can''t start a style-file command');
  2740. lower_case (buffer, buf_ptr1, token_len);       {ignore case differences}
  2741. command_num := ilk_info[
  2742.         str_lookup(buffer,buf_ptr1,token_len,bst_command_ilk,dont_insert)];
  2743. if (not hash_found) then
  2744.     begin
  2745.     print_token;
  2746.     bst_err (' is an illegal style-file command');
  2747.     end;
  2748. @<Process the appropriate \.{.bst} command@>;
  2749. exit:
  2750. @^style-file commands@>
  2751. @:this can't happen}{\quad Unknown style-file command@>
  2752. Here we determine which \.{.bst} command we're about to process, and
  2753. then go to it.
  2754. @<Process the appropriate \.{.bst} command@>=
  2755. case (command_num) of
  2756.     n_bst_entry : bst_entry_command;
  2757.     n_bst_execute : bst_execute_command;
  2758.     n_bst_function : bst_function_command;
  2759.     n_bst_integers : bst_integers_command;
  2760.     n_bst_iterate : bst_iterate_command;
  2761.     n_bst_macro : bst_macro_command;
  2762.     n_bst_read : bst_read_command;
  2763.     n_bst_reverse : bst_reverse_command;
  2764.     n_bst_sort : bst_sort_command;
  2765.     n_bst_strings : bst_strings_command;
  2766.     othercases confusion ('Unknown style-file command')
  2767. endcases
  2768. We need data structures for the function definitions, the entry
  2769. variables, the global variables, and the actual entries corresponding
  2770. to the cite-key list.  First we define the classes of `function's
  2771. used.  Functions in all classes are of |bst_fn_ilk| except for
  2772. |int_literal|s, which are of |integer_ilk|; and |str_literal|s, which
  2773. are of |text_ilk|.
  2774. @d built_in = 0         {the `primitive' functions}
  2775. @d wiz_defined = 1      {defined in the \.{.bst} file}
  2776. @d int_literal = 2      {integer `constants'}
  2777. @d str_literal = 3      {string `constants'}
  2778. @d field = 4            {things like `author' and `title'}
  2779. @d int_entry_var = 5    {integer entry variable}
  2780. @d str_entry_var = 6    {string entry variable}
  2781. @d int_global_var = 7   {integer global variable}
  2782. @d str_global_var = 8   {string global variable}
  2783. @d last_fn_class = 8    {the same number as on the line above}
  2784. @:this can't happen}{\quad Unknown function class@>
  2785. Here's another bug report.
  2786. @<Procedures and functions for all file I/O, error messages, and such@>=
  2787. procedure unknwn_function_class_confusion;
  2788. begin
  2789. confusion ('Unknown function class');
  2790. @:this can't happen}{\quad Unknown function class@>
  2791. Occasionally we'll want to |print| the name of one of these function
  2792. classes.
  2793. @<Procedures and functions for all file I/O, error messages, and such@>=
  2794. procedure print_fn_class (@!fn_loc : hash_loc);
  2795. begin
  2796. case (fn_type[fn_loc]) of
  2797.     built_in : print ('built-in');
  2798.     wiz_defined : print ('wizard-defined');
  2799.     int_literal : print ('integer-literal');
  2800.     str_literal : print ('string-literal');
  2801.     field : print ('field');
  2802.     int_entry_var : print ('integer-entry-variable');
  2803.     str_entry_var : print ('string-entry-variable');
  2804.     int_global_var : print ('integer-global-variable');
  2805.     str_global_var : print ('string-global-variable');
  2806.     othercases unknwn_function_class_confusion
  2807. endcases;
  2808. @:this can't happen}{\quad Unknown function class@>
  2809. This version is for printing when in |trace| mode.
  2810. @<Procedures and functions for all file I/O, error messages, and such@>=
  2811.   trace
  2812.   procedure trace_pr_fn_class (@!fn_loc : hash_loc);
  2813.   begin
  2814.   case (fn_type[fn_loc]) of
  2815.     built_in : trace_pr ('built-in');
  2816.     wiz_defined : trace_pr ('wizard-defined');
  2817.     int_literal : trace_pr ('integer-literal');
  2818.     str_literal : trace_pr ('string-literal');
  2819.     field : trace_pr ('field');
  2820.     int_entry_var : trace_pr ('integer-entry-variable');
  2821.     str_entry_var : trace_pr ('string-entry-variable');
  2822.     int_global_var : trace_pr ('integer-global-variable');
  2823.     str_global_var : trace_pr ('string-global-variable');
  2824.     othercases unknwn_function_class_confusion
  2825.   endcases;
  2826.   end;
  2827.   ecart
  2828. Besides the function classes, we have types based on \BibTeX's
  2829. capacity limitations and one based on what can go into the array
  2830. |wiz_functions| explained below.
  2831. @d quote_next_fn = hash_base - 1  {special marker used in defining functions}
  2832. @d end_of_def = hash_max + 1      {another such special marker}
  2833. @<Types in the outer block@>=
  2834. @!fn_class = 0..last_fn_class;          {the \.{.bst} function classes}
  2835. @!wiz_fn_loc = 0..wiz_fn_space;  {|wiz_defined|-function storage locations}
  2836. @!int_ent_loc = 0..max_ent_ints;        {|int_entry_var| storage locations}
  2837. @!str_ent_loc = 0..max_ent_strs;        {|str_entry_var| storage locations}
  2838. @!str_glob_loc = 0..max_glb_str_minus_1; {|str_global_var| storage locations}
  2839. @!field_loc = 0..max_fields;            {individual field storage locations}
  2840. @!hash_ptr2 = quote_next_fn..end_of_def; {a special marker or a |hash_loc|}
  2841. @^save space@>
  2842. @^space savings@>
  2843. @^system dependencies@>
  2844. We store information about the \.{.bst} functions in arrays the same
  2845. size as the hash-table arrays and in locations corresponding to their
  2846. hash-table locations.  The two arrays |fn_info| (an alias of
  2847. |ilk_info| described earlier) and |fn_type| accomplish this: |fn_type|
  2848. specifies one of the above classes, and |fn_info| gives information
  2849. dependent on the class.
  2850. Six other arrays give the contents of functions: The array
  2851. |wiz_functions| holds definitions for |wiz_defined| functions---each
  2852. such function consists of a sequence of pointers to hash-table
  2853. locations of other functions (with the two special-marker exceptions
  2854. above); the array |entry_ints| contains the current values of
  2855. |int_entry_var|s; the array |entry_strs| contains the current values
  2856. of |str_entry_var|s; an element of the array |global_strs| contains
  2857. the current value of a |str_global_var| if the corresponding
  2858. |glb_str_ptr| entry is empty, otherwise the nonempty entry is a
  2859. pointer to the string; and the array |field_info|, for each field of
  2860. each entry, contains either a pointer to the string or the special
  2861. value |missing|.
  2862. The array |global_strs| isn't packed (that is, it isn't |array| \dots\
  2863. |of packed array| \dots$\,$) to increase speed on some systems;
  2864. however, on systems that are byte-addressable and that have a good
  2865. compiler, packing |global_strs| would save lots of space without much
  2866. loss of speed.
  2867. @d fn_info == ilk_info          {an alias used with functions}
  2868. @d missing = empty              {a special pointer for missing fields}
  2869. @<Globals in the outer block@>=
  2870. @!fn_loc : hash_loc;            {the hash-table location of a function}
  2871. @!wiz_loc : hash_loc;           {the hash-table location of a wizard function}
  2872. @!literal_loc : hash_loc;       {the hash-table location of a literal function}
  2873. @!macro_name_loc : hash_loc;    {the hash-table location of a macro name}
  2874. @!macro_def_loc : hash_loc;     {the hash-table location of a macro definition}
  2875. @!fn_type : packed array[hash_loc] of fn_class;
  2876. @!wiz_def_ptr : wiz_fn_loc;     {storage location for the next wizard function}
  2877. @!wiz_fn_ptr : wiz_fn_loc;      {general |wiz_functions| location}
  2878. @!wiz_functions : packed array[wiz_fn_loc] of hash_ptr2;
  2879. @!int_ent_ptr : int_ent_loc;    {general |int_entry_var| location}
  2880. @!entry_ints : array[int_ent_loc] of integer;
  2881. @!num_ent_ints : int_ent_loc;   {the number of distinct |int_entry_var| names}
  2882. @!str_ent_ptr : str_ent_loc;    {general |str_entry_var| location}
  2883. @!entry_strs : array[str_ent_loc] of
  2884.                                 packed array[0..ent_str_size] of ASCII_code;
  2885. @!num_ent_strs : str_ent_loc;   {the number of distinct |str_entry_var| names}
  2886. @!str_glb_ptr : 0..max_glob_strs;       {general |str_global_var| location}
  2887. @!glb_str_ptr : array[str_glob_loc] of str_number;
  2888. @!global_strs : array[str_glob_loc] of array[0..glob_str_size] of ASCII_code;
  2889. @!glb_str_end : array[str_glob_loc] of 0..glob_str_size;        {end markers}
  2890. @!num_glb_strs : 0..max_glob_strs; {number of distinct |str_global_var| names}
  2891. @!field_ptr : field_loc;        {general |field_info| location}
  2892. @!field_parent_ptr,@!field_end_ptr : field_loc; {two more for doing cross-refs}
  2893. @!cite_parent_ptr,@!cite_xptr : cite_number;  {two others for doing cross-refs}
  2894. @!field_info : packed array[field_loc] of str_number;
  2895. @!num_fields : field_loc;       {the number of distinct field names}
  2896. @!num_pre_defined_fields : field_loc;   {so far, just one: \.{crossref}}
  2897. @!crossref_num : field_loc;     {the number given to \.{crossref}}
  2898. @!no_fields : boolean;          {used for |tr_print|ing entry information}
  2899. Now we initialize storage for the |wiz_defined| functions and we
  2900. initialize variables so that the first |str_entry_var|,
  2901. |int_entry_var|, |str_global_var|, and |field| name will be assigned
  2902. the number~0.  Note: The variables |num_ent_strs| and |num_fields|
  2903. will also be set when pre-defining strings.
  2904. @<Set initial values of key variables@>=
  2905. wiz_def_ptr := 0;
  2906. num_ent_ints := 0;
  2907. num_ent_strs := 0;
  2908. num_fields := 0;
  2909. str_glb_ptr := 0;
  2910. while (str_glb_ptr < max_glob_strs) do          {make |str_global_var|s empty}
  2911.     begin
  2912.     glb_str_ptr[str_glb_ptr] := 0;
  2913.     glb_str_end[str_glb_ptr] := 0;
  2914.     incr(str_glb_ptr);
  2915.     end;
  2916. num_glb_strs := 0;
  2917. @* Style-file commands.
  2918. @^style-file commands@>
  2919. There are ten \.{.bst} commands: Five (\.{entry}, \.{function},
  2920. \.{integers}, \.{macro}, and \.{strings}) declare and define
  2921. functions, one (\.{read}) reads in the \.{.bib}-file entries, and four
  2922. (\.{execute}, \.{iterate}, \.{reverse}, and \.{sort})
  2923. manipulate the entries and produce output.
  2924. The boolean variables |entry_seen| and |read_seen| indicate whether
  2925. we've yet encountered an \.{entry} and a \.{read} command.  There must
  2926. be exactly one of each of these, and the \.{entry} command, as well as
  2927. any \.{macro} command, must precede the \.{read} command.
  2928. Furthermore, the \.{read} command must precede the four that
  2929. manipulate the entries and produce output.
  2930. @<Globals in the outer block@>=
  2931. @!entry_seen : boolean; {|true| if we've already seen an \.{entry} command}
  2932. @!read_seen : boolean;  {|true| if we've already seen a \.{read} command}
  2933. @!read_performed : boolean; {|true| if we started reading the database file(s)}
  2934. @!reading_completed : boolean; {|true| if we made it all the way through}
  2935. @!read_completed : boolean; {|true| if the database info didn't bomb \BibTeX}
  2936. And we initialize them.
  2937. @<Set initial values of key variables@>=
  2938. entry_seen := false;
  2939. read_seen := false;
  2940. read_performed := false;
  2941. reading_completed := false;
  2942. read_completed := false;
  2943. @:this can't happen}{\quad Identifier scanning error@>
  2944. Here's another bug.
  2945. @<Procedures and functions for all file I/O, error messages, and such@>=
  2946. procedure id_scanning_confusion;
  2947. begin
  2948. confusion ('Identifier scanning error');
  2949. @:this can't happen}{\quad Identifier scanning error@>
  2950. This macro is used to scan all \.{.bst} identifiers.  The argument
  2951. supplies the \.{.bst} command name.  The associated procedure simply
  2952. prints an error message.
  2953. @d bst_identifier_scan(#) ==
  2954.         begin
  2955.         scan_identifier (right_brace,comment,comment);
  2956.         if ((scan_result = white_adjacent) or
  2957.                                 (scan_result = specified_char_adjacent)) then
  2958.             do_nothing
  2959.         else
  2960.             begin
  2961.             bst_id_print;
  2962.             bst_err (#);
  2963.             end;
  2964.         end
  2965. @<Procedures and functions for all file I/O, error messages, and such@>=
  2966. procedure bst_id_print;
  2967. begin
  2968. if (scan_result = id_null) then
  2969.     print ('"',xchr[scan_char],'" begins identifier, command: ')
  2970. else if (scan_result = other_char_adjacent) then
  2971.     print ('"',xchr[scan_char],'" immediately follows identifier, command: ')
  2972.     id_scanning_confusion;
  2973. This macro just makes sure we're at a |left_brace|.
  2974. @d bst_get_and_check_left_brace(#) ==
  2975.         begin
  2976.         if (scan_char <> left_brace) then
  2977.             begin
  2978.             bst_left_brace_print;
  2979.             bst_err (#);
  2980.             end;
  2981.         incr(buf_ptr2);                 {skip over the |left_brace|}
  2982.         end
  2983. @<Procedures and functions for all file I/O, error messages, and such@>=
  2984. procedure bst_left_brace_print;
  2985. begin
  2986. print ('"',xchr[left_brace],'" is missing in command: ');
  2987. And this one, a |right_brace|.
  2988. @d bst_get_and_check_right_brace(#) ==
  2989.         begin
  2990.         if (scan_char <> right_brace) then
  2991.             begin
  2992.             bst_right_brace_print;
  2993.             bst_err (#);
  2994.             end;
  2995.         incr(buf_ptr2);                 {skip over the |right_brace|}
  2996.         end
  2997. @<Procedures and functions for all file I/O, error messages, and such@>=
  2998. procedure bst_right_brace_print;
  2999. begin
  3000. print ('"',xchr[right_brace],'" is missing in command: ');
  3001. This macro complains if we've already encountered a function to be
  3002. inserted into the hash table.
  3003. @d check_for_already_seen_function(#) ==
  3004.         begin
  3005.         if (hash_found) then  {already encountered this as a \.{.bst} function}
  3006.             begin
  3007.             already_seen_function_print (#);
  3008.             return;
  3009.             end;
  3010.         end
  3011. @<Procedures and functions for all file I/O, error messages, and such@>=
  3012. procedure already_seen_function_print (@!seen_fn_loc : hash_loc);
  3013. label exit;     {so the call to |bst_err| works}
  3014. begin
  3015. print_pool_str (hash_text[seen_fn_loc]);
  3016. print (' is already a type "');
  3017. print_fn_class (seen_fn_loc);
  3018. print_ln ('" function name');
  3019. bst_err_print_and_look_for_blank_line_return;
  3020. exit:
  3021. @:style-file commands}{\quad \.{entry}@>
  3022. An \.{entry} command has three arguments, each a (possibly empty) list
  3023. of function names between braces (the names are separated by one or
  3024. more |white_space| characters).  All function names in this and other
  3025. commands must be legal \.{.bst} identifiers.  Upper/lower cases are
  3026. considered to be the same for function names in these lists---all
  3027. upper-case letters are converted to lower case.  These arguments give
  3028. lists of |field|s, |int_entry_var|s, and |str_entry_var|s.
  3029. @<Procedures and functions for the reading and processing of input files@>=
  3030. procedure bst_entry_command;
  3031. label exit;
  3032. begin
  3033. if (entry_seen) then
  3034.     bst_err ('Illegal, another entry command');
  3035. entry_seen := true;             {now we've seen an \.{entry} command}
  3036. eat_bst_white_and_eof_check ('entry');
  3037. @<Scan the list of |field|s@>;
  3038. eat_bst_white_and_eof_check ('entry');
  3039. if (num_fields = num_pre_defined_fields) then
  3040.     bst_warn ('Warning--I didn''t find any fields');
  3041. @<Scan the list of |int_entry_var|s@>;
  3042. eat_bst_white_and_eof_check ('entry');
  3043. @<Scan the list of |str_entry_var|s@>;
  3044. exit:
  3045. This module reads a |left_brace|, the list of |field|s, and a
  3046. |right_brace|.  The |field|s are those like `author' and `title.'
  3047. @<Scan the list of |field|s@>=
  3048. begin
  3049. bst_get_and_check_left_brace ('entry');
  3050. eat_bst_white_and_eof_check ('entry');
  3051. while (scan_char <> right_brace) do
  3052.     begin
  3053.     bst_identifier_scan ('entry');
  3054.     @<Insert a |field| into the hash table@>;
  3055.     eat_bst_white_and_eof_check ('entry');
  3056.     end;
  3057. incr(buf_ptr2);                 {skip over the |right_brace|}
  3058. @^secret agent man@>
  3059. Here we insert the just found field name into the hash table, record
  3060. it as a |field|, and assign it a number to be used in indexing into
  3061. the |field_info| array.
  3062. @<Insert a |field| into the hash table@>=
  3063. begin
  3064.   trace
  3065.   trace_pr_token;
  3066.   trace_pr_ln (' is a field');
  3067.   ecart@/
  3068. lower_case (buffer, buf_ptr1, token_len);       {ignore case differences}
  3069. fn_loc := str_lookup(buffer,buf_ptr1,token_len,bst_fn_ilk,do_insert);
  3070. check_for_already_seen_function (fn_loc);
  3071. fn_type[fn_loc] := field;@/
  3072. fn_info[fn_loc] := num_fields;  {give this field a number (take away its name)}
  3073. incr(num_fields);
  3074. This module reads a |left_brace|, the list of |int_entry_var|s,
  3075. and a |right_brace|.
  3076. @<Scan the list of |int_entry_var|s@>=
  3077. begin
  3078. bst_get_and_check_left_brace ('entry');
  3079. eat_bst_white_and_eof_check ('entry');
  3080. while (scan_char <> right_brace) do
  3081.     begin
  3082.     bst_identifier_scan ('entry');
  3083.     @<Insert an |int_entry_var| into the hash table@>;
  3084.     eat_bst_white_and_eof_check ('entry');
  3085.     end;
  3086. incr(buf_ptr2);                 {skip over the |right_brace|}
  3087. Here we insert the just found |int_entry_var| name into the hash table
  3088. and record it as an |int_entry_var|.  An |int_entry_var| is one that
  3089. the style designer wants a separate copy of for each entry.
  3090. @<Insert an |int_entry_var| into the hash table@>=
  3091. begin
  3092.   trace
  3093.   trace_pr_token;
  3094.   trace_pr_ln (' is an integer entry-variable');
  3095.   ecart@/
  3096. lower_case (buffer, buf_ptr1, token_len);       {ignore case differences}
  3097. fn_loc := str_lookup(buffer,buf_ptr1,token_len,bst_fn_ilk,do_insert);
  3098. check_for_already_seen_function (fn_loc);
  3099. fn_type[fn_loc] := int_entry_var;@/
  3100. fn_info[fn_loc] := num_ent_ints;        {give this |int_entry_var| a number}
  3101. incr(num_ent_ints);
  3102. This module reads a |left_brace|, the list of |str_entry_var|s, and a
  3103. |right_brace|.  A |str_entry_var| is one that the style designer wants
  3104. a separate copy of for each entry.
  3105. @<Scan the list of |str_entry_var|s@>=
  3106. begin
  3107. bst_get_and_check_left_brace ('entry');
  3108. eat_bst_white_and_eof_check ('entry');
  3109. while (scan_char <> right_brace) do
  3110.     begin
  3111.     bst_identifier_scan ('entry');
  3112.     @<Insert a |str_entry_var| into the hash table@>;
  3113.     eat_bst_white_and_eof_check ('entry');
  3114.     end;
  3115. incr(buf_ptr2);                 {skip over the |right_brace|}
  3116. Here we insert the just found |str_entry_var| name into the hash
  3117. table, record it as a |str_entry_var|, and set its pointer into
  3118. |entry_strs|.
  3119. @<Insert a |str_entry_var| into the hash table@>=
  3120. begin
  3121.   trace
  3122.   trace_pr_token;
  3123.   trace_pr_ln (' is a string entry-variable');
  3124.   ecart@/
  3125. lower_case (buffer, buf_ptr1, token_len);       {ignore case differences}
  3126. fn_loc := str_lookup(buffer,buf_ptr1,token_len,bst_fn_ilk,do_insert);
  3127. check_for_already_seen_function (fn_loc);
  3128. fn_type[fn_loc] := str_entry_var;@/
  3129. fn_info[fn_loc] := num_ent_strs;        {give this |str_entry_var| a number}
  3130. incr(num_ent_strs);
  3131. A legal argument for an \.{execute}, \.{iterate}, or \.{reverse}
  3132. command must exist and be |built_in| or |wiz_defined|.
  3133. Here's where we check, returning |true| if the argument is illegal.
  3134. @<Procedures and functions for the reading and processing of input files@>=
  3135. function bad_argument_token : boolean;
  3136. label exit;
  3137. begin
  3138. bad_argument_token := true;     {now it's easy to exit if necessary}
  3139. lower_case (buffer, buf_ptr1, token_len);       {ignore case differences}
  3140. fn_loc := str_lookup(buffer,buf_ptr1,token_len,bst_fn_ilk,dont_insert);
  3141. if (not hash_found) then                        {unknown \.{.bst} function}
  3142.     begin
  3143.     print_token;
  3144.     bst_err (' is an unknown function');
  3145.     end
  3146. else if ((fn_type[fn_loc] <> built_in) and
  3147.          (fn_type[fn_loc] <> wiz_defined)) then
  3148.     begin
  3149.     print_token;
  3150.     print (' has bad function type ');
  3151.     print_fn_class (fn_loc);
  3152.     bst_err_print_and_look_for_blank_line_return;
  3153.     end;
  3154. bad_argument_token := false;
  3155. exit:
  3156. @:style-file commands}{\quad \.{execute}@>
  3157. An \.{execute} command has one argument, a single |built_in| or
  3158. |wiz_defined| function name between braces.  Upper/lower cases are
  3159. considered to be the same---all upper-case letters are converted to
  3160. lower case.  Also, we must make sure we've already seen a \.{read}
  3161. command.
  3162. This module reads a |left_brace|, a single function to be executed,
  3163. and a |right_brace|.
  3164. @<Procedures and functions for the reading and processing of input files@>=
  3165. procedure bst_execute_command;
  3166. label exit;
  3167. begin
  3168. if (not read_seen) then
  3169.     bst_err ('Illegal, execute command before read command');
  3170. eat_bst_white_and_eof_check ('execute');
  3171. bst_get_and_check_left_brace ('execute');
  3172. eat_bst_white_and_eof_check ('execute');
  3173. bst_identifier_scan ('execute');
  3174. @<Check the \.{execute}-command argument token@>;
  3175. eat_bst_white_and_eof_check ('execute');
  3176. bst_get_and_check_right_brace ('execute');
  3177. @<Perform an \.{execute} command@>;
  3178. exit:
  3179. Before executing the function, we must make sure it's a legal one.  It
  3180. must exist and be |built_in| or |wiz_defined|.
  3181. @<Check the \.{execute}-command argument token@>=
  3182. begin
  3183.   trace
  3184.   trace_pr_token;
  3185.   trace_pr_ln (' is a to be executed function');
  3186.   ecart@/
  3187. if (bad_argument_token) then
  3188.     return;
  3189. @:style-file commands}{\quad \.{function}@>
  3190. A \.{function} command has two arguments; the first is a
  3191. |wiz_defined| function name between braces.  Upper/lower cases are
  3192. considered to be the same---all upper-case letters are converted to
  3193. lower case.  The second argument defines this function.  It consists
  3194. of a sequence of functions, between braces, separated by |white_space|
  3195. characters.  Upper/lower cases are considered to be the same for
  3196. function names but not for |str_literal|s.
  3197. @<Procedures and functions for the reading and processing of input files@>=
  3198. procedure bst_function_command;
  3199. label exit;
  3200. begin
  3201. eat_bst_white_and_eof_check ('function');
  3202. @<Scan the |wiz_defined| function name@>;
  3203. eat_bst_white_and_eof_check ('function');
  3204. bst_get_and_check_left_brace ('function');
  3205. scan_fn_def(wiz_loc);           {this scans the function definition}
  3206. exit:
  3207. This module reads a |left_brace|, a |wiz_defined| function name, and
  3208. a |right_brace|.
  3209. @<Scan the |wiz_defined| function name@>=
  3210. begin
  3211. bst_get_and_check_left_brace ('function');
  3212. eat_bst_white_and_eof_check ('function');
  3213. bst_identifier_scan ('function');
  3214. @<Check the |wiz_defined| function name@>;
  3215. eat_bst_white_and_eof_check ('function');
  3216. bst_get_and_check_right_brace ('function');
  3217. The function name must exist and be a new one; we mark it as
  3218. |wiz_defined|.  Also, see if it's the default entry-type function.
  3219. @<Check the |wiz_defined| function name@>=
  3220. begin
  3221.   trace
  3222.   trace_pr_token;
  3223.   trace_pr_ln (' is a wizard-defined function');
  3224.   ecart@/
  3225. lower_case (buffer, buf_ptr1, token_len);       {ignore case differences}
  3226. wiz_loc := str_lookup(buffer,buf_ptr1,token_len,bst_fn_ilk,do_insert);
  3227. check_for_already_seen_function (wiz_loc);
  3228. fn_type[wiz_loc] := wiz_defined;
  3229. if (hash_text[wiz_loc] = s_default) then  {we've found the default entry-type}
  3230.     b_default := wiz_loc;       {see the |built_in| functions for |b_default|}
  3231. We're about to start scanning tokens in a function definition.  When a
  3232. function token is illegal, we skip until it ends; a |white_space|
  3233. character, an end-of-line, a |right_brace|, or a |comment| marks the
  3234. end of the current token.
  3235. @d next_token=25                {a bad function token; go read the next one}
  3236. @d skip_token(#) == begin       {not-so-serious error during \.{.bst} parsing}
  3237.                     print (#);
  3238.                     skip_token_print;   {also, skip to the current token's end}
  3239.                     goto next_token;
  3240.                     end
  3241. @<Procedures and functions for input scanning@>=
  3242. procedure skip_token_print;
  3243. begin
  3244. print ('-');
  3245. bst_ln_num_print;
  3246. mark_error;
  3247. if (scan2_white(right_brace,comment)) then              {ok if token ends line}
  3248.     do_nothing;
  3249. @^commented-out code@>
  3250. @^for a good time, try comment-out code@>
  3251. This macro is similar to the last one but is specifically for
  3252. recursion in a |wiz_defined| function, which is illegal; it helps save
  3253. space.
  3254. @d skip_recursive_token == begin
  3255.                            print_recursion_illegal;
  3256.                            goto next_token;
  3257.                            end
  3258. @<Procedures and functions for input scanning@>=
  3259. procedure print_recursion_illegal;
  3260. begin
  3261.   trace
  3262.   trace_pr_newline;
  3263.   ecart@/
  3264. print_ln ('Curse you, wizard, before you recurse me:');
  3265. print ('function ');
  3266. print_token;
  3267. print_ln (' is illegal in its own definition');
  3268.   print_recursion_illegal;
  3269.   @}@/
  3270. skip_token_print;                       {also, skip to the current token's end}
  3271. Here's another macro for saving some space when there's a problem with
  3272. a token.
  3273. @d skip_token_unknown_function == begin
  3274.                                   skp_token_unknown_function_print;
  3275.                                   goto next_token;
  3276.                                   end
  3277. @<Procedures and functions for input scanning@>=
  3278. procedure skp_token_unknown_function_print;
  3279. begin
  3280. print_token;
  3281. print (' is an unknown function');
  3282. skip_token_print;                       {also, skip to the current token's end}
  3283. And another.
  3284. @d skip_token_illegal_stuff_after_literal ==
  3285.                         begin
  3286.                         skip_illegal_stuff_after_token_print;
  3287.                         goto next_token;
  3288.                         end
  3289. @<Procedures and functions for input scanning@>=
  3290. procedure skip_illegal_stuff_after_token_print;
  3291. begin
  3292. print ('"',xchr[scan_char],'" can''t follow a literal');
  3293. skip_token_print;                       {also, skip to the current token's end}
  3294. This recursive function reads and stores the list of functions
  3295. (separated by |white_space| characters or ends-of-line) that define
  3296. this new function, and reads a |right_brace|.
  3297. @<Procedures and functions for input scanning@>=
  3298. procedure scan_fn_def (@!fn_hash_loc : hash_loc);
  3299. label next_token,@!exit;
  3300. type @!fn_def_loc = 0..single_fn_space; {for a single |wiz_defined|-function}
  3301. var singl_function : packed array[fn_def_loc] of hash_ptr2;
  3302.     @!single_ptr : fn_def_loc;  {next storage location for this definition}
  3303.     @!copy_ptr : fn_def_loc;    {dummy variable}
  3304.     @!end_of_num : buf_pointer; {the end of an implicit function's name}
  3305.     @!impl_fn_loc : hash_loc;   {an implicit function's hash-table location}
  3306. begin
  3307. eat_bst_white_and_eof_check ('function');
  3308. single_ptr := 0;
  3309. while (scan_char <> right_brace) do
  3310.     begin
  3311.     @<Get the next function of the definition@>;
  3312. next_token:
  3313.     eat_bst_white_and_eof_check ('function');
  3314.     end;
  3315. @<Complete this function's definition@>;
  3316. incr(buf_ptr2);                 {skip over the |right_brace|}
  3317. exit:
  3318. @:BibTeX capacity exceeded}{\quad single function space@>
  3319. This macro inserts a hash-table location (or one of the two
  3320. special markers |quote_next_fn| and |end_of_def|) into the
  3321. |singl_function| array, which will later be copied into the
  3322. |wiz_functions| array.
  3323. @d insert_fn_loc(#) ==  begin
  3324.                         singl_function[single_ptr] := #;
  3325.                         if (single_ptr = single_fn_space) then
  3326.                             singl_fn_overflow;
  3327.                         incr(single_ptr);
  3328.                         end
  3329. @<Procedures and functions for all file I/O, error messages, and such@>=
  3330. procedure singl_fn_overflow;
  3331. begin
  3332. overflow('single function space ',single_fn_space);
  3333. There are five possibilities for the first character of the token
  3334. representing the next function of the definition: If it's a
  3335. |number_sign|, the token is an |int_literal|; if it's a
  3336. |double_quote|, the token is a |str_literal|; if it's a
  3337. |single_quote|, the token is a quoted function; if it's a
  3338. |left_brace|, the token isn't really a token, but rather the start of
  3339. another function definition (which will result in a recursive call to
  3340. |scan_fn_def|); if it's anything else, the token is the name of an
  3341. already-defined function.  Note: To prevent the wizard from using
  3342. recursion, we have to check that neither a quoted function nor an
  3343. already-defined-function is actually the currently-being-defined
  3344. function (which is stored at |wiz_loc|).
  3345. @<Get the next function of the definition@>=
  3346. case (scan_char) of
  3347.     number_sign : @<Scan an |int_literal|@>;
  3348.     double_quote : @<Scan a |str_literal|@>;
  3349.     single_quote : @<Scan a quoted function@>;
  3350.     left_brace : @<Start a new function definition@>;
  3351.     othercases @<Scan an already-defined function@>
  3352. endcases
  3353. An |int_literal| is preceded by a |number_sign|, consists of an
  3354. integer (i.e., an optional |minus_sign| followed by one or more
  3355. |numeric| characters), and is followed either by a |white_space|
  3356. character, an end-of-line, or a |right_brace|.  The array |fn_info|
  3357. contains the value of the integer for |int_literal|s.
  3358. @<Scan an |int_literal|@>=
  3359. begin
  3360. incr(buf_ptr2);                         {skip over the |number_sign|}
  3361. if (not scan_integer) then
  3362.     skip_token ('Illegal integer in integer literal');
  3363.   trace
  3364.   trace_pr ('#');
  3365.   trace_pr_token;
  3366.   trace_pr_ln (' is an integer literal with value ',token_value:0);
  3367.   ecart@/
  3368. literal_loc := str_lookup(buffer,buf_ptr1,token_len,integer_ilk,do_insert);
  3369. if (not hash_found) then
  3370.     begin
  3371.     fn_type[literal_loc] := int_literal;        {set the |fn_class|}
  3372.     fn_info[literal_loc] := token_value;        {the value of this integer}
  3373.     end;
  3374. if ((lex_class[scan_char]<>white_space) and (buf_ptr2<last) and
  3375.             (scan_char<>right_brace) and@| (scan_char<>comment)) then
  3376.     skip_token_illegal_stuff_after_literal;
  3377. insert_fn_loc (literal_loc);    {add this function to |wiz_functions|}
  3378. A |str_literal| is preceded by a |double_quote| and consists of all
  3379. characters on this line up to the next |double_quote|.  Also, there
  3380. must be either a |white_space| character, an end-of-line, a
  3381. |right_brace|, or a |comment| following (since functions in the
  3382. definition must be separated by |white_space|).  The array |fn_info|
  3383. contains nothing for |str_literal|s.
  3384. @<Scan a |str_literal|@>=
  3385. begin
  3386. incr(buf_ptr2);                         {skip over the |double_quote|}
  3387. if (not scan1(double_quote)) then
  3388.     skip_token ('No `',xchr[double_quote],''' to end string literal');
  3389.   trace
  3390.   trace_pr ('"');
  3391.   trace_pr_token;
  3392.   trace_pr ('"');
  3393.   trace_pr_ln (' is a string literal');
  3394.   ecart@/
  3395. literal_loc := str_lookup(buffer,buf_ptr1,token_len,text_ilk,do_insert);@/
  3396. fn_type[literal_loc] := str_literal;    {set the |fn_class|}
  3397. incr(buf_ptr2);                         {skip over the |double_quote|}
  3398. if ((lex_class[scan_char]<>white_space) and (buf_ptr2<last) and
  3399.         (scan_char<>right_brace) and@| (scan_char<>comment)) then
  3400.     skip_token_illegal_stuff_after_literal;
  3401. insert_fn_loc (literal_loc);            {add this function to |wiz_functions|}
  3402. A quoted function is preceded by a |single_quote| and consists of all
  3403. characters up to the next |white_space| character, end-of-line,
  3404. |right_brace|, or |comment|.
  3405. @<Scan a quoted function@>=
  3406. begin
  3407. incr(buf_ptr2);                                 {skip over the |single_quote|}
  3408. if (scan2_white(right_brace,comment)) then              {ok if token ends line}
  3409.     do_nothing;
  3410.   trace
  3411.   trace_pr ('''');
  3412.   trace_pr_token;
  3413.   trace_pr (' is a quoted function ');
  3414.   ecart@/
  3415. lower_case (buffer, buf_ptr1, token_len);       {ignore case differences}
  3416. fn_loc := str_lookup(buffer,buf_ptr1,token_len,bst_fn_ilk,dont_insert);
  3417. if (not hash_found) then                        {unknown \.{.bst} function}
  3418.     skip_token_unknown_function
  3419.     @<Check and insert the quoted function@>;
  3420. Here we check that this quoted function is a legal one---the function
  3421. name must already exist, but it mustn't be the currently-being-defined
  3422. function (which is stored at |wiz_loc|).
  3423. @<Check and insert the quoted function@>=
  3424. begin
  3425. if (fn_loc = wiz_loc) then
  3426.     skip_recursive_token
  3427.     begin
  3428.       trace
  3429.       trace_pr ('of type ');
  3430.       trace_pr_fn_class (fn_loc);
  3431.       trace_pr_newline;
  3432.       ecart@/
  3433.     insert_fn_loc (quote_next_fn);      {add special marker together with}
  3434.     insert_fn_loc (fn_loc);             {this function to |wiz_functions|}
  3435.     end
  3436. @^kludge@>
  3437. @:this can't happen}{\quad Already encountered implicit function@>
  3438. This module marks the implicit function as being quoted, generates a
  3439. name, and stores it in the hash table.  This name is strictly internal
  3440. to this program, starts with a |single_quote| (since that will make
  3441. this function name unique), and ends with the variable |impl_fn_num|
  3442. converted to ASCII.  The alias kludge helps make the stack space not
  3443. overflow on some machines.
  3444. @d ex_buf2 == ex_buf            {an alias, used only in this module}
  3445. @<Start a new function definition@>=
  3446. begin
  3447. ex_buf2[0] := single_quote;
  3448. int_to_ASCII (impl_fn_num,ex_buf2,1,end_of_num);
  3449. impl_fn_loc := str_lookup(ex_buf2,0,end_of_num,bst_fn_ilk,do_insert);
  3450. if (hash_found) then
  3451.     confusion ('Already encountered implicit function');
  3452.   trace
  3453.   trace_pr_pool_str (hash_text[impl_fn_loc]);
  3454.   trace_pr_ln (' is an implicit function');
  3455.   ecart@/
  3456. incr(impl_fn_num);
  3457. fn_type[impl_fn_loc] := wiz_defined;@/
  3458. insert_fn_loc (quote_next_fn);  {all implicit functions are quoted}
  3459. insert_fn_loc (impl_fn_loc);    {add it to |wiz_functions|}
  3460. incr(buf_ptr2);                 {skip over the |left_brace|}
  3461. scan_fn_def (impl_fn_loc);      {this is the recursive call}
  3462. The variable |impl_fn_num| counts the number of implicit functions
  3463. seen in the \.{.bst} file.
  3464. @<Globals in the outer block@>=
  3465. @!impl_fn_num : integer;        {the number of implicit functions seen so far}
  3466. Now we initialize it.
  3467. @<Set initial values of key variables@>=
  3468. impl_fn_num := 0;
  3469. @:BibTeX capacity exceeded}{\quad buffer size@>
  3470. This module appends a character to |int_buf| after checking to make
  3471. sure it will fit; for use in |int_to_ASCII|.
  3472. @d append_int_char(#) == begin
  3473.                          if (int_ptr = buf_size) then
  3474.                              buffer_overflow;
  3475.                          int_buf[int_ptr]:=#;
  3476.                          incr(int_ptr);
  3477.                          end
  3478. This procedure takes the integer |int|, copies the appropriate
  3479. |ASCII_code| string into |int_buf| starting at |int_begin|, and sets
  3480. the |var| parameter |int_end| to the first unused |int_buf| location.
  3481. The ASCII string will consist of decimal digits, the first of which
  3482. will be not be a~0 if the integer is nonzero, with a prepended minus
  3483. sign if the integer is negative.
  3484. @<Procedures and functions for handling numbers, characters, and strings@>=
  3485. procedure int_to_ASCII (@!int:integer; var int_buf:buf_type;
  3486.                         @!int_begin:buf_pointer; var int_end:buf_pointer);
  3487. var int_ptr,@!int_xptr : buf_pointer;   {pointers into |int_buf|}
  3488.   @!int_tmp_val : ASCII_code;           {the temporary element in an exchange}
  3489. begin
  3490. int_ptr := int_begin;
  3491. if (int < 0) then       {add the |minus_sign| and use the absolute value}
  3492.     begin
  3493.     append_int_char (minus_sign);
  3494.     int := -int;
  3495.     end;
  3496. int_xptr := int_ptr;
  3497. repeat                          {copy digits into |int_buf|}
  3498.     append_int_char ("0" + (int mod 10));
  3499.     int := int div 10;
  3500.   until (int = 0);
  3501. int_end := int_ptr;             {set the string length}
  3502. decr(int_ptr);
  3503. while (int_xptr < int_ptr) do   {and reorder (flip) the digits}
  3504.     begin
  3505.     int_tmp_val := int_buf[int_xptr];
  3506.     int_buf[int_xptr] := int_buf[int_ptr];
  3507.     int_buf[int_ptr] := int_tmp_val;
  3508.     decr(int_ptr);
  3509.     incr(int_xptr);
  3510.     end
  3511. An already-defined function consists of all characters up to the next
  3512. |white_space| character, end-of-line, |right_brace|, or |comment|.
  3513. This function name must already exist, but it mustn't be the
  3514. currently-being-defined function (which is stored at |wiz_loc|).
  3515. @<Scan an already-defined function@>=
  3516. begin
  3517. if (scan2_white(right_brace,comment)) then              {ok if token ends line}
  3518.     do_nothing;
  3519.   trace
  3520.   trace_pr_token;
  3521.   trace_pr (' is a function ');
  3522.   ecart@/
  3523. lower_case (buffer, buf_ptr1, token_len);       {ignore case differences}
  3524. fn_loc := str_lookup(buffer,buf_ptr1,token_len,bst_fn_ilk,dont_insert);
  3525. if (not hash_found) then                        {unknown \.{.bst} function}
  3526.     skip_token_unknown_function
  3527. else if (fn_loc = wiz_loc) then
  3528.     skip_recursive_token
  3529.     begin
  3530.       trace
  3531.       trace_pr ('of type ');
  3532.       trace_pr_fn_class (fn_loc);
  3533.       trace_pr_newline;
  3534.       ecart@/
  3535.     insert_fn_loc (fn_loc);     {add this function to |wiz_functions|}
  3536.     end;
  3537. @:BibTeX capacity exceeded}{\quad wizard-defined function space@>
  3538. Now we add the |end_of_def| special marker, make sure this function will
  3539. fit into |wiz_functions|, and put it there.
  3540. @<Complete this function's definition@>=
  3541. begin
  3542. insert_fn_loc (end_of_def);  {add special marker ending the definition}
  3543. if (single_ptr + wiz_def_ptr > wiz_fn_space) then
  3544.     begin
  3545.     print (single_ptr + wiz_def_ptr : 0,': ');
  3546.     overflow('wizard-defined function space ',wiz_fn_space);
  3547.     end;
  3548. fn_info[fn_hash_loc] := wiz_def_ptr;            {pointer into |wiz_functions|}
  3549. copy_ptr := 0;
  3550. while (copy_ptr < single_ptr) do                {make this function official}
  3551.     begin
  3552.     wiz_functions[wiz_def_ptr] := singl_function[copy_ptr];
  3553.     incr(copy_ptr);
  3554.     incr(wiz_def_ptr);
  3555.     end;
  3556. @:style-file commands}{\quad \.{integers}@>
  3557. An \.{integers} command has one argument, a list of function names
  3558. between braces (the names are separated by one or more |white_space|
  3559. characters).  Upper/lower cases are considered to be the same for
  3560. function names in these lists---all upper-case letters are converted to
  3561. lower case.  Each name in this list specifies an |int_global_var|.
  3562. There may be several \.{integers} commands in the \.{.bst} file.
  3563. This module reads a |left_brace|, a list of |int_global_var|s, and a
  3564. |right_brace|.
  3565. @<Procedures and functions for the reading and processing of input files@>=
  3566. procedure bst_integers_command;
  3567. label exit;
  3568. begin
  3569. eat_bst_white_and_eof_check ('integers');
  3570. bst_get_and_check_left_brace ('integers');
  3571. eat_bst_white_and_eof_check ('integers');
  3572. while (scan_char <> right_brace) do
  3573.     begin
  3574.     bst_identifier_scan ('integers');
  3575.     @<Insert an |int_global_var| into the hash table@>;
  3576.     eat_bst_white_and_eof_check ('integers');
  3577.     end;
  3578. incr(buf_ptr2);                 {skip over the |right_brace|}
  3579. exit:
  3580. Here we insert the just found |int_global_var| name into the hash
  3581. table and record it as an |int_global_var|.  Also, we initialize it by
  3582. setting |fn_info[fn_loc]| to 0.
  3583. @<Insert an |int_global_var| into the hash table@>=
  3584. begin
  3585.   trace
  3586.   trace_pr_token;
  3587.   trace_pr_ln (' is an integer global-variable');
  3588.   ecart@/
  3589. lower_case (buffer, buf_ptr1, token_len);       {ignore case differences}
  3590. fn_loc := str_lookup(buffer,buf_ptr1,token_len,bst_fn_ilk,do_insert);
  3591. check_for_already_seen_function (fn_loc);
  3592. fn_type[fn_loc] := int_global_var;@/
  3593. fn_info[fn_loc] := 0;                           {initialize}
  3594. @:style-file commands}{\quad \.{iterate}@>
  3595. An \.{iterate} command has one argument, a single |built_in| or
  3596. |wiz_defined| function name between braces.  Upper/lower cases are
  3597. considered to be the same---all upper-case letters are converted to
  3598. lower case.  Also, we must make sure we've already seen a \.{read}
  3599. command.
  3600. This module reads a |left_brace|, a single function to be iterated,
  3601. and a |right_brace|.
  3602. @<Procedures and functions for the reading and processing of input files@>=
  3603. procedure bst_iterate_command;
  3604. label exit;
  3605. begin
  3606. if (not read_seen) then
  3607.     bst_err ('Illegal, iterate command before read command');
  3608. eat_bst_white_and_eof_check ('iterate');
  3609. bst_get_and_check_left_brace ('iterate');
  3610. eat_bst_white_and_eof_check ('iterate');
  3611. bst_identifier_scan ('iterate');
  3612. @<Check the \.{iterate}-command argument token@>;
  3613. eat_bst_white_and_eof_check ('iterate');
  3614. bst_get_and_check_right_brace ('iterate');
  3615. @<Perform an \.{iterate} command@>;
  3616. exit:
  3617. Before iterating the function, we must make sure it's a legal one.  It
  3618. must exist and be |built_in| or |wiz_defined|.
  3619. @<Check the \.{iterate}-command argument token@>=
  3620. begin
  3621.   trace
  3622.   trace_pr_token;
  3623.   trace_pr_ln (' is a to be iterated function');
  3624.   ecart@/
  3625. if (bad_argument_token) then
  3626.     return;
  3627. @:style-file commands}{\quad \.{macro}@>
  3628. A \.{macro} command, like a \.{function} command, has two arguments;
  3629. the first is a macro name between braces.  The name must be a legal
  3630. \.{.bst} identifier.  Upper/lower cases are considered to be the
  3631. same---all upper-case letters are converted to lower case.  The second
  3632. argument defines this macro.  It consists of a
  3633. |double_quote|-delimited string (which must be on a single line)
  3634. between braces, with optional |white_space| characters between the
  3635. braces and the |double_quote|s.  This |double_quote|-delimited string
  3636. is parsed exactly as a |str_literal| is for the \.{function} command.
  3637. @<Procedures and functions for the reading and processing of input files@>=
  3638. procedure bst_macro_command;
  3639. label exit;
  3640. begin
  3641. if (read_seen) then
  3642.     bst_err ('Illegal, macro command after read command');
  3643. eat_bst_white_and_eof_check ('macro');
  3644. @<Scan the macro name@>;
  3645. eat_bst_white_and_eof_check ('macro');
  3646. @<Scan the macro's definition@>;
  3647. exit:
  3648. This module reads a |left_brace|, a macro name, and a |right_brace|.
  3649. @<Scan the macro name@>=
  3650. begin
  3651. bst_get_and_check_left_brace ('macro');
  3652. eat_bst_white_and_eof_check ('macro');
  3653. bst_identifier_scan ('macro');
  3654. @<Check the macro name@>;
  3655. eat_bst_white_and_eof_check ('macro');
  3656. bst_get_and_check_right_brace ('macro');
  3657. The macro name must be a new one; we mark it as |macro_ilk|.
  3658. @<Check the macro name@>=
  3659. begin
  3660.   trace
  3661.   trace_pr_token;
  3662.   trace_pr_ln (' is a macro');
  3663.   ecart@/
  3664. lower_case (buffer, buf_ptr1, token_len);       {ignore case differences}
  3665. macro_name_loc := str_lookup(buffer,buf_ptr1,token_len,macro_ilk,do_insert);
  3666. if (hash_found) then
  3667.     begin
  3668.     print_token;
  3669.     bst_err (' is already defined as a macro');
  3670.     end;
  3671. ilk_info[macro_name_loc]:=hash_text[macro_name_loc]; {default in case of error}
  3672. This module reads a |left_brace|, the |double_quote|-delimited string
  3673. that defines this macro, and a |right_brace|.
  3674. @<Scan the macro's definition@>=
  3675. begin
  3676. bst_get_and_check_left_brace ('macro');
  3677. eat_bst_white_and_eof_check ('macro');
  3678. if (scan_char <> double_quote) then
  3679.     bst_err ('A macro definition must be ',xchr[double_quote],'-delimited');
  3680. @<Scan the macro definition-string@>;
  3681. eat_bst_white_and_eof_check ('macro');
  3682. bst_get_and_check_right_brace ('macro');
  3683. A macro definition-string is preceded by a |double_quote| and consists
  3684. of all characters on this line up to the next |double_quote|.  The
  3685. array |ilk_info| contains a pointer to this string for the macro name.
  3686. @<Scan the macro definition-string@>=
  3687. begin
  3688. incr(buf_ptr2);                         {skip over the |double_quote|}
  3689. if (not scan1(double_quote)) then
  3690.     bst_err ('There''s no `',xchr[double_quote],''' to end macro definition');
  3691.   trace
  3692.   trace_pr ('"');
  3693.   trace_pr_token;
  3694.   trace_pr ('"');
  3695.   trace_pr_ln (' is a macro string');
  3696.   ecart@/
  3697. macro_def_loc := str_lookup(buffer,buf_ptr1,token_len,text_ilk,do_insert);@/
  3698. fn_type[macro_def_loc] := str_literal;  {set the |fn_class|}
  3699. ilk_info[macro_name_loc] := hash_text[macro_def_loc];
  3700. incr(buf_ptr2);                         {skip over the |double_quote|}
  3701. @^gymnastics@>
  3702. We need to include stuff for \.{.bib} reading here because that's done
  3703. by the \.{read} command.
  3704. @<Procedures and functions for the reading and processing of input files@>=
  3705. @<Scan for and process a \.{.bib} command or database entry@>
  3706. @:style-file commands}{\quad \.{read}@>
  3707. The \.{read} command has no arguments so there's no more parsing to
  3708. do.  We must make sure we haven't seen a \.{read} command before and
  3709. we've already seen an \.{entry} command.
  3710. @<Procedures and functions for the reading and processing of input files@>=
  3711. procedure bst_read_command;
  3712. label exit;
  3713. begin
  3714. if (read_seen) then
  3715.     bst_err ('Illegal, another read command');
  3716. read_seen := true;              {now we've seen a \.{read} command}
  3717. if (not entry_seen) then
  3718.     bst_err ('Illegal, read command before entry command');
  3719. sv_ptr1 := buf_ptr2;            {save the contents of the \.{.bst} input line}
  3720. sv_ptr2 := last;
  3721. tmp_ptr := sv_ptr1;
  3722. while (tmp_ptr < sv_ptr2) do
  3723.     begin
  3724.     sv_buffer[tmp_ptr] := buffer[tmp_ptr];
  3725.     incr(tmp_ptr);
  3726.     end;
  3727. @<Read the \.{.bib} file(s)@>;
  3728. buf_ptr2 := sv_ptr1;            {and restore}
  3729. last := sv_ptr2;
  3730. tmp_ptr := buf_ptr2;
  3731. while (tmp_ptr < last) do
  3732.     begin
  3733.     buffer[tmp_ptr] := sv_buffer[tmp_ptr];
  3734.     incr(tmp_ptr);
  3735.     end;
  3736. exit:
  3737. @:style-file commands}{\quad \.{reverse}@>
  3738. A \.{reverse} command has one argument, a single |built_in| or
  3739. |wiz_defined| function name between braces.  Upper/lower cases are
  3740. considered to be the same---all upper-case letters are converted to
  3741. lower case.  Also, we must make sure we've already seen a \.{read}
  3742. command.
  3743. This module reads a |left_brace|, a single function to be iterated in
  3744. reverse, and a |right_brace|.
  3745. @<Procedures and functions for the reading and processing of input files@>=
  3746. procedure bst_reverse_command;
  3747. label exit;
  3748. begin
  3749. if (not read_seen) then
  3750.     bst_err ('Illegal, reverse command before read command');
  3751. eat_bst_white_and_eof_check ('reverse');
  3752. bst_get_and_check_left_brace ('reverse');
  3753. eat_bst_white_and_eof_check ('reverse');
  3754. bst_identifier_scan ('reverse');
  3755. @<Check the \.{reverse}-command argument token@>;
  3756. eat_bst_white_and_eof_check ('reverse');
  3757. bst_get_and_check_right_brace ('reverse');
  3758. @<Perform a \.{reverse} command@>;
  3759. exit:
  3760. Before iterating the function in reverse, we must make sure it's a
  3761. legal one.  It must exist and be |built_in| or |wiz_defined|.
  3762. @<Check the \.{reverse}-command argument token@>=
  3763. begin
  3764.   trace
  3765.   trace_pr_token;
  3766.   trace_pr_ln (' is a to be iterated in reverse function');
  3767.   ecart@/
  3768. if (bad_argument_token) then
  3769.     return;
  3770. @:style-file commands}{\quad \.{sort}@>
  3771. The \.{sort} command has no arguments so there's no more parsing to
  3772. do, but we must make sure we've already seen a \.{read} command.
  3773. @<Procedures and functions for the reading and processing of input files@>=
  3774. procedure bst_sort_command;
  3775. label exit;
  3776. begin
  3777. if (not read_seen) then
  3778.     bst_err ('Illegal, sort command before read command');
  3779. @<Perform a \.{sort} command@>;
  3780. exit:
  3781. @:style-file commands}{\quad \.{strings}@>
  3782. A \.{strings} command has one argument, a list of function names
  3783. between braces (the names are separated by one or more |white_space|
  3784. characters).  Upper/lower cases are considered to be the same for
  3785. function names in these lists---all upper-case letters are converted to
  3786. lower case.  Each name in this list specifies a |str_global_var|.
  3787. There may be several \.{strings} commands in the \.{.bst} file.
  3788. This module reads a |left_brace|, a list of |str_global_var|s,
  3789. and a |right_brace|.
  3790. @<Procedures and functions for the reading and processing of input files@>=
  3791. procedure bst_strings_command;
  3792. label exit;
  3793. begin
  3794. eat_bst_white_and_eof_check ('strings');
  3795. bst_get_and_check_left_brace ('strings');
  3796. eat_bst_white_and_eof_check ('strings');
  3797. while (scan_char <> right_brace) do
  3798.     begin
  3799.     bst_identifier_scan ('strings');
  3800.     @<Insert a |str_global_var| into the hash table@>;
  3801.     eat_bst_white_and_eof_check ('strings');
  3802.     end;
  3803. incr(buf_ptr2);                 {skip over the |right_brace|}
  3804. exit:
  3805. @:BibTeX capacity exceeded}{\quad number of string global-variables@>
  3806. Here we insert the just found |str_global_var| name into the hash
  3807. table, record it as a |str_global_var|, set its pointer into
  3808. |global_strs|, and initialize its value there to the null string.
  3809. @d end_of_string = invalid_code  {this illegal |ASCII_code| ends a string}
  3810. @<Insert a |str_global_var| into the hash table@>=
  3811. begin
  3812.   trace
  3813.   trace_pr_token;
  3814.   trace_pr_ln (' is a string global-variable');
  3815.   ecart@/
  3816. lower_case (buffer, buf_ptr1, token_len);       {ignore case differences}
  3817. fn_loc := str_lookup(buffer,buf_ptr1,token_len,bst_fn_ilk,do_insert);
  3818. check_for_already_seen_function (fn_loc);
  3819. fn_type[fn_loc] := str_global_var;@/
  3820. fn_info[fn_loc] := num_glb_strs;                {pointer into |global_strs|}
  3821. if (num_glb_strs = max_glob_strs) then
  3822.     overflow('number of string global-variables ',max_glob_strs);
  3823. incr(num_glb_strs);
  3824. @^gymnastics@>
  3825. That's it for processing \.{.bst} commands, except for finishing the
  3826. procedural gymnastics.  Note that this must topologically follow the
  3827. stuff for \.{.bib} reading, because that's done by the \.{.bst}'s
  3828. \.{read} command.
  3829. @<Procedures and functions for the reading and processing of input files@>=
  3830. @<Scan for and process a \.{.bst} command@>
  3831. @* Reading the database file(s).
  3832. This section reads the \.{.bib} file(s), each of which consists of a
  3833. sequence of entries (perhaps with a few \.{.bib} commands thrown in,
  3834. as explained later).  Each entry consists of an |at_sign|, an entry
  3835. type, and, between braces or parentheses and separated by |comma|s, a
  3836. database key and a list of fields.  Each field consists of a field
  3837. name, an |equals_sign|, and nonempty list of field tokens separated by
  3838. |concat_char|s.  Each field token is either a nonnegative number, a
  3839. macro name (like `jan'), or a brace-balanced string delimited by
  3840. either |double_quote|s or braces.  Finally, case differences are
  3841. ignored for all but delimited strings and database keys, and
  3842. |white_space| characters and ends-of-line may appear in all reasonable
  3843. places (i.e., anywhere except within entry types, database keys, field
  3844. names, and macro names); furthermore, comments may appear anywhere
  3845. between entries (or before the first or after the last) as long as
  3846. they contain no |at_sign|s.
  3847. These global variables are used while reading the \.{.bib} file(s).
  3848. The elements of |type_list|, which indicate an entry's type (book,
  3849. article, etc.), point either to a |hash_loc| or are one of two special
  3850. markers: |empty|, from which |hash_base = empty + 1| was defined,
  3851. means we haven't yet encountered the \.{.bib} entry corresponding to
  3852. this cite key; and |undefined| means we've encountered it but it had
  3853. an unknown entry type.  Thus the array |type_list| is of type
  3854. |hash_ptr2|, also defined earlier.  An element of the boolean array
  3855. |entry_exists| whose corresponding entry in |cite_list| gets
  3856. overwritten (which happens only when |all_entries| is |true|)
  3857. indicates whether we've encountered that entry of |cite_list| while
  3858. reading the \.{.bib} file(s); this information is unused for entries
  3859. that aren't (or more precisely, that have no chance of being)
  3860. overwritten.  When we're reading the database file, the array
  3861. |cite_info| contains auxiliary information for |cite_list|.  Later,
  3862. |cite_info| will become |sorted_cites|, and this dual role imposes the
  3863. (not-very-imposing) restriction |max_strings >= max_cites|.
  3864. @d undefined = hash_max + 1     {a special marker used for |type_list|}
  3865. @<Globals in the outer block@>=
  3866. @!bib_line_num : integer;       {line number of the \.{.bib} file}
  3867. @!entry_type_loc : hash_loc;    {the hash-table location of an entry type}
  3868. @!type_list : packed array[cite_number] of hash_ptr2;
  3869. @!type_exists : boolean;        {|true| if this entry type is \.{.bst}-defined}
  3870. @!entry_exists : packed array[cite_number] of boolean;
  3871. @!store_entry : boolean;        {|true| if we're to store info for this entry}
  3872. @!field_name_loc : hash_loc;    {the hash-table location of a field name}
  3873. @!field_val_loc : hash_loc;     {the hash-table location of a field value}
  3874. @!store_field : boolean;        {|true| if we're to store info for this field}
  3875. @!store_token : boolean;        {|true| if we're to store this macro token}
  3876. @!right_outer_delim : ASCII_code; {either a |right_brace| or a |right_paren|}
  3877. @!right_str_delim : ASCII_code; {either a |right_brace| or a |double_quote|}
  3878. @!at_bib_command : boolean;     {|true| for a command, false for an entry}
  3879. @!cur_macro_loc : hash_loc;     {|macro_loc| for a \.{string} being defined}
  3880. @!cite_info : packed array[cite_number] of str_number; {extra |cite_list| info}
  3881. @!cite_hash_found : boolean;    {set to a previous |hash_found| value}
  3882. @!preamble_ptr : bib_number;    {pointer into the |s_preamble| array}
  3883. @!num_preamble_strings : bib_number;    {counts the |s_preamble| strings}
  3884. This little procedure exists because it's used by at least two other
  3885. procedures and thus saves some space.
  3886. @<Procedures and functions for all file I/O, error messages, and such@>=
  3887. procedure bib_ln_num_print;
  3888. begin
  3889. print ('--line ',bib_line_num:0,' of file ');
  3890. print_bib_name;
  3891. When there's a serious error parsing a \.{.bib} file, we flush
  3892. everything up to the beginning of the next entry.
  3893. @d bib_err(#) == begin          {serious error during \.{.bib} parsing}
  3894.                  print (#);
  3895.                  bib_err_print;
  3896.                  return;
  3897.                  end
  3898. @<Procedures and functions for all file I/O, error messages, and such@>=
  3899. procedure bib_err_print;
  3900. begin
  3901. print ('-');
  3902. bib_ln_num_print;
  3903. print_bad_input_line;                   {this call does the |mark_error|}
  3904. print_skipping_whatever_remains;
  3905. if (at_bib_command) then
  3906.     print_ln ('command')
  3907.   else
  3908.     print_ln ('entry');
  3909. When there's a harmless error parsing a \.{.bib} file, we just give a
  3910. warning message.  This is always called after other stuff has been
  3911. printed out.
  3912. @d bib_warn(#) == begin         {non-serious error during \.{.bst} parsing}
  3913.                   print (#);
  3914.                   bib_warn_print;
  3915.                   end
  3916. @d bib_warn_newline(#) == begin         {same as above but with a newline}
  3917.                           print_ln (#);
  3918.                           bib_warn_print;
  3919.                           end
  3920. @<Procedures and functions for all file I/O, error messages, and such@>=
  3921. procedure bib_warn_print;
  3922. begin
  3923. bib_ln_num_print;
  3924. mark_warning;
  3925. For all |num_bib_files| database files, we keep reading and processing
  3926. \.{.bib} entries until none left.
  3927. @<Read the \.{.bib} file(s)@>=
  3928. begin
  3929. @<Final initialization for \.{.bib} processing@>;
  3930. read_performed := true;
  3931. bib_ptr := 0;
  3932. while (bib_ptr < num_bib_files) do
  3933.     begin
  3934.     print ('Database file #',bib_ptr+1:0,': ');
  3935.     print_bib_name;@/
  3936.     bib_line_num := 0;          {initialize to get the first input line}
  3937.     buf_ptr2 := last;
  3938.     while (not eof(cur_bib_file)) do
  3939.         get_bib_command_or_entry_and_process;
  3940.     a_close (cur_bib_file);
  3941.     incr(bib_ptr);
  3942.     end;
  3943. reading_completed := true;
  3944.   trace
  3945.   trace_pr_ln ('Finished reading the database file(s)');
  3946.   ecart@/
  3947. @<Final initialization for processing the entries@>;
  3948. read_completed := true;
  3949. We need to initialize the |field_info| array, and also various things
  3950. associated with the |cite_list| array (but not |cite_list| itself).
  3951. @<Final initialization for \.{.bib} processing@>=
  3952. begin
  3953. @<Initialize the |field_info|@>;
  3954. @<Initialize things for the |cite_list|@>;
  3955. This module initializes all fields of all entries to |missing|, the
  3956. value to which all fields are initialized.
  3957. @<Initialize the |field_info|@>=
  3958. begin
  3959. check_field_overflow (num_fields*num_cites);
  3960. field_ptr := 0;
  3961. while (field_ptr < max_fields) do
  3962.     begin
  3963.     field_info[field_ptr] := missing;
  3964.     incr(field_ptr);
  3965.     end;
  3966. @^fetish@>
  3967. @:BibTeX capacity exceeded}{\quad total number of fields@>
  3968. Complain if somebody's got a field fetish.
  3969. @<Procedures and functions for all file I/O, error messages, and such@>=
  3970. procedure check_field_overflow (@!total_fields : integer);
  3971. begin
  3972. if (total_fields > max_fields) then
  3973.     begin
  3974.     print_ln (total_fields:0,' fields:');
  3975.     overflow('total number of fields ',max_fields);
  3976.     end;
  3977. We must initialize the |type_list| array so that we can detect
  3978. duplicate (or missing) entries for cite keys on |cite_list|.  Also,
  3979. when we're to include the entire database, we use the array
  3980. |entry_exists| to detect those missing entries whose |cite_list| info
  3981. will (or to be more precise, might) be overwritten; and we use the
  3982. array |cite_info| to save the part of |cite_list| that will (might) be
  3983. overwritten.  We also use |cite_info| for counting cross~references
  3984. when it's appropriate---when an entry isn't otherwise to be included
  3985. on |cite_list| (that is, the entry isn't \.{\\cite}d or
  3986. \.{\\nocite}d).  Such an entry is included on the final |cite_list| if
  3987. it's cross~referenced at least |min_crossrefs| times.
  3988. @<Initialize things for the |cite_list|@>=
  3989. begin
  3990. cite_ptr := 0;
  3991. while (cite_ptr < max_cites) do
  3992.     begin
  3993.     type_list[cite_ptr] := empty;@/
  3994.     cite_info[cite_ptr] := any_value;  {to appeas \PASCAL's boolean evaluation}
  3995.     incr(cite_ptr);
  3996.     end;
  3997. old_num_cites := num_cites;
  3998. if (all_entries) then
  3999.     begin
  4000.     cite_ptr := all_marker;
  4001.     while (cite_ptr < old_num_cites) do
  4002.         begin
  4003.         cite_info[cite_ptr] := cite_list[cite_ptr];
  4004.         entry_exists[cite_ptr] := false;
  4005.         incr(cite_ptr);
  4006.         end;
  4007.     cite_ptr := all_marker;     {we insert the ``other'' entries here}
  4008.     end
  4009.   else
  4010.     begin
  4011.     cite_ptr := num_cites;      {we insert the cross-referenced entries here}
  4012.     all_marker := any_value;    {to appease \PASCAL's boolean evaluation}
  4013.     end;
  4014. Before we actually start the code for reading a database file, we must
  4015. define this \.{.bib}-specific scanning function.  It skips over
  4016. |white_space| characters until hitting a nonwhite character or the end
  4017. of the file, respectively returning |true| or |false|.  It also
  4018. updates |bib_line_num|, the line counter.
  4019. @<Procedures and functions for input scanning@>=
  4020. function eat_bib_white_space : boolean;
  4021. label exit;
  4022. begin
  4023. while (not scan_white_space) do         {no characters left; read another line}
  4024.     begin
  4025.     if (not input_ln(cur_bib_file)) then        {end-of-file; return |false|}
  4026.         begin
  4027.         eat_bib_white_space := false;
  4028.         return;
  4029.         end;
  4030.     incr(bib_line_num);
  4031.     buf_ptr2 := 0;
  4032.     end;
  4033. eat_bib_white_space := true;
  4034. exit:
  4035. It's often illegal to end a \.{.bib} command in certain places, and
  4036. this is where we come to check.
  4037. @d eat_bib_white_and_eof_check ==
  4038.         begin
  4039.         if (not eat_bib_white_space) then
  4040.             begin
  4041.             eat_bib_print;
  4042.             return;
  4043.             end;
  4044.         end
  4045. @<Procedures and functions for all file I/O, error messages, and such@>=
  4046. procedure eat_bib_print;
  4047. label exit;     {so the call to |bib_err| works}
  4048. begin
  4049. bib_err ('Illegal end of database file');
  4050. exit:
  4051. And here are a bunch of error-message macros, each called more than
  4052. once, that thus save space as implemented.  This one is for when one
  4053. of two possible characters is expected while scanning.
  4054. @d bib_one_of_two_expected_err(#) ==
  4055.         begin
  4056.         bib_one_of_two_print (#);
  4057.         return;
  4058.         end
  4059. @<Procedures and functions for all file I/O, error messages, and such@>=
  4060. procedure bib_one_of_two_print (@!char1,@!char2:ASCII_code);
  4061. label exit;     {so the call to |bib_err| works}
  4062. begin
  4063. bib_err ('I was expecting a `',xchr[char1],''' or a `',xchr[char2],'''');
  4064. exit:
  4065. This one's for an expected |equals_sign|.
  4066. @d bib_equals_sign_expected_err ==
  4067.         begin
  4068.         bib_equals_sign_print;
  4069.         return;
  4070.         end
  4071. @<Procedures and functions for all file I/O, error messages, and such@>=
  4072. procedure bib_equals_sign_print;
  4073. label exit;     {so the call to |bib_err| works}
  4074. begin
  4075. bib_err ('I was expecting an "',xchr[equals_sign],'"');
  4076. exit:
  4077. This complains about unbalanced braces.
  4078. @d bib_unbalanced_braces_err ==
  4079.         begin
  4080.         bib_unbalanced_braces_print;
  4081.         return;
  4082.         end
  4083. @<Procedures and functions for all file I/O, error messages, and such@>=
  4084. procedure bib_unbalanced_braces_print;
  4085. label exit;     {so the call to |bib_err| works}
  4086. begin
  4087. bib_err ('Unbalanced braces');
  4088. exit:
  4089. And this one about an overly exuberant field.
  4090. @d bib_field_too_long_err ==
  4091.         begin
  4092.         bib_field_too_long_print;
  4093.         return;
  4094.         end
  4095. @<Procedures and functions for all file I/O, error messages, and such@>=
  4096. procedure bib_field_too_long_print;
  4097. label exit;     {so the call to |bib_err| works}
  4098. begin
  4099. bib_err ('Your field is more than ',buf_size:0,' characters');
  4100. exit:
  4101. This one is just a warning, not an error.  It's for when something
  4102. isn't (or might not be) quite right with a macro name.
  4103. @d macro_name_warning(#) ==
  4104.         begin
  4105.         macro_warn_print;
  4106.         bib_warn_newline (#);
  4107.         end
  4108. @<Procedures and functions for all file I/O, error messages, and such@>=
  4109. procedure macro_warn_print;
  4110. begin
  4111. print ('Warning--string name "');
  4112. print_token;
  4113. print ('" is ');
  4114. @:this can't happen}{\quad Identifier scanning error@>
  4115. This macro is used to scan all \.{.bib} identifiers.  The argument
  4116. tells what was happening at the time.  The associated procedure simply
  4117. prints an error message.
  4118. @d bib_identifier_scan_check(#) ==
  4119.         begin
  4120.         if ((scan_result = white_adjacent) or
  4121.                                 (scan_result = specified_char_adjacent)) then
  4122.             do_nothing
  4123.         else
  4124.             begin
  4125.             bib_id_print;
  4126.             bib_err (#);
  4127.             end;
  4128.         end
  4129. @<Procedures and functions for all file I/O, error messages, and such@>=
  4130. procedure bib_id_print;
  4131. begin
  4132. if (scan_result = id_null) then
  4133.     print ('You''re missing ')
  4134. else if (scan_result = other_char_adjacent) then
  4135.     print ('"',xchr[scan_char],'" immediately follows ')
  4136.     id_scanning_confusion;
  4137. This module either reads a database entry, whose three main components
  4138. are an entry type, a database key, and a list of fields, or it reads a
  4139. \.{.bib} command, whose structure is command dependent and explained
  4140. later.
  4141. @d cite_already_set = 22        {this gets around \PASCAL\ limitations}
  4142. @d first_time_entry = 26        {for checking for repeated database entries}
  4143. @<Scan for and process a \.{.bib} command or database entry@>=
  4144. procedure get_bib_command_or_entry_and_process;
  4145. label cite_already_set,@!first_time_entry,@!loop_exit,@!exit;
  4146. begin
  4147. at_bib_command := false;@/
  4148. @<Skip to the next database entry or \.{.bib} command@>;
  4149. @<Scan the entry type or scan and process the \.{.bib} command@>;
  4150. eat_bib_white_and_eof_check;
  4151. @<Scan the entry's database key@>;
  4152. eat_bib_white_and_eof_check;
  4153. @<Scan the entry's list of fields@>;
  4154. exit:
  4155. This module skips over everything until hitting an |at_sign| or the
  4156. end of the file.  It also updates |bib_line_num|, the line counter.
  4157. @<Skip to the next database entry or \.{.bib} command@>=
  4158. while (not scan1(at_sign)) do                   {no |at_sign|; get next line}
  4159.     begin
  4160.     if (not input_ln(cur_bib_file)) then        {end-of-file}
  4161.         return;
  4162.     incr(bib_line_num);
  4163.     buf_ptr2 := 0;
  4164.     end
  4165. @:this can't happen}{\quad An at-sign disappeared@>
  4166. This module reads an |at_sign| and an entry type (like `book' or
  4167. `article') or a \.{.bib} command.  If it's an entry type, it must be
  4168. defined in the \.{.bst} file if this entry is to be included in the
  4169. reference list.
  4170. @<Scan the entry type or scan and process the \.{.bib} command@>=
  4171. begin
  4172. if (scan_char <> at_sign) then
  4173.     confusion ('An "',xchr[at_sign],'" disappeared');
  4174. incr(buf_ptr2);                                 {skip over the |at_sign|}
  4175. eat_bib_white_and_eof_check;
  4176. scan_identifier (left_brace,left_paren,left_paren);
  4177. bib_identifier_scan_check ('an entry type');
  4178.   trace
  4179.   trace_pr_token;
  4180.   trace_pr_ln (' is an entry type or a database-file command');
  4181.   ecart@/
  4182. lower_case (buffer, buf_ptr1, token_len);       {ignore case differences}
  4183. command_num := ilk_info[
  4184.         str_lookup(buffer,buf_ptr1,token_len,bib_command_ilk,dont_insert)];
  4185. if (hash_found) then
  4186.     @<Process a \.{.bib} command@>
  4187.     begin                                       {process an entry type}
  4188.     entry_type_loc := str_lookup(
  4189.                         buffer,buf_ptr1,token_len,bst_fn_ilk,dont_insert);
  4190.     if ((not hash_found) or (fn_type[entry_type_loc]<>wiz_defined)) then@/
  4191.         type_exists := false  {no such entry type defined in the \.{.bst} file}
  4192.       else
  4193.         type_exists := true;
  4194.     end;
  4195. @^database-file commands@>
  4196. @:this can't happen}{\quad Unknown database-file command@>
  4197. Here we determine which \.{.bib} command we're about to process, then
  4198. go to it.
  4199. @<Process a \.{.bib} command@>=
  4200. begin
  4201. at_bib_command := true;
  4202. case (command_num) of
  4203.     n_bib_comment : @<Process a \.{comment} command@>;
  4204.     n_bib_preamble : @<Process a \.{preamble} command@>;
  4205.     n_bib_string : @<Process a \.{string} command@>;
  4206.     othercases bib_cmd_confusion
  4207. endcases;
  4208. @:this can't happen}{\quad Unknown database-file command@>
  4209. Here's another bug.
  4210. @<Procedures and functions for all file I/O, error messages, and such@>=
  4211. procedure bib_cmd_confusion;
  4212. begin
  4213. confusion ('Unknown database-file command');
  4214. @:database-file commands}{\quad \.{comment}@>
  4215. The \.{comment} command is implemented for SCRIBE compatibility.  It's
  4216. not really needed because \BibTeX\ treats (flushes) everything not
  4217. within an entry as a comment anyway.
  4218. @<Process a \.{comment} command@>=
  4219. begin
  4220. return;                 {flush comments}
  4221. @:database-file commands}{\quad \.{preamble}@>
  4222. The \.{preamble} command lets a user have \TeX\ stuff inserted (by the
  4223. standard styles, at least) directly into the \.{.bbl} file.  It is
  4224. intended primarily for allowing \TeX\ macro definitions used within
  4225. the bibliography entries (for better sorting, for example).  One
  4226. \.{preamble} command per \.{.bib} file should suffice.
  4227. A \.{preamble} command has either braces or parentheses as outer
  4228. delimiters.  Inside is the preamble string, which has the same syntax
  4229. as a field value: a nonempty list of field tokens separated by
  4230. |concat_char|s.  There are three types of field tokens---nonnegative
  4231. numbers, macro names, and delimited strings.
  4232. This module does all the scanning (that's not subcontracted), but the
  4233. \.{.bib}-specific scanning function
  4234. |scan_and_store_the_field_value_and_eat_white| actually stores the
  4235. value.
  4236. @<Process a \.{preamble} command@>=
  4237. begin
  4238. if (preamble_ptr = max_bib_files) then
  4239.     bib_err ('You''ve exceeded ',max_bib_files:0,' preamble commands');
  4240. eat_bib_white_and_eof_check;
  4241. if (scan_char = left_brace) then
  4242.     right_outer_delim := right_brace
  4243. else if (scan_char = left_paren) then
  4244.     right_outer_delim := right_paren
  4245.     bib_one_of_two_expected_err (left_brace,left_paren);
  4246. incr(buf_ptr2);                         {skip over the left-delimiter}
  4247. eat_bib_white_and_eof_check;
  4248. store_field := true;
  4249. if (not scan_and_store_the_field_value_and_eat_white) then
  4250.     return;
  4251. if (scan_char <> right_outer_delim) then
  4252.     bib_err ('Missing "',xchr[right_outer_delim],'" in preamble command');
  4253. incr(buf_ptr2);                         {skip over the |right_outer_delim|}
  4254. return;
  4255. @:database-file commands}{\quad \.{string}@>
  4256. The \.{string} command is implemented both for SCRIBE compatibility
  4257. and for allowing a user: to override a \.{.bst}-file \.{macro}
  4258. command, to define one that the \.{.bst} file doesn't, or to engage in
  4259. good, wholesome, typing laziness.
  4260. The \.{string} command does mostly the same thing as the
  4261. \.{.bst}-file's \.{macro} command (but the syntax is different and the
  4262. \.{string} command compresses |white_space|).  In fact, later in this
  4263. program, the term ``macro'' refers to either a \.{.bst} ``macro'' or a
  4264. \.{.bib} ``string'' (when it's clear from the context that it's not
  4265. a \.{WEB} macro).
  4266. A \.{string} command has either braces or parentheses as outer
  4267. delimiters.  Inside is the string's name (it must be a legal
  4268. identifier, and case differences are ignored---all upper-case letters
  4269. are converted to lower case), then an |equals_sign|, and the string's
  4270. definition, which has the same syntax as a field value: a nonempty
  4271. list of field tokens separated by |concat_char|s.  There are three
  4272. types of field tokens---nonnegative numbers, macro names, and
  4273. delimited strings.
  4274. @<Process a \.{string} command@>=
  4275. begin
  4276. eat_bib_white_and_eof_check;
  4277. @<Scan the string's name@>;
  4278. eat_bib_white_and_eof_check;
  4279. @<Scan the string's definition field@>;
  4280. return;
  4281. This module reads a left outer-delimiter and a string name.
  4282. @<Scan the string's name@>=
  4283. begin
  4284. if (scan_char = left_brace) then
  4285.     right_outer_delim := right_brace
  4286. else if (scan_char = left_paren) then
  4287.     right_outer_delim := right_paren
  4288.     bib_one_of_two_expected_err (left_brace,left_paren);
  4289. incr(buf_ptr2);                         {skip over the left-delimiter}
  4290. eat_bib_white_and_eof_check;
  4291. scan_identifier (equals_sign,equals_sign,equals_sign);
  4292. bib_identifier_scan_check ('a string name');
  4293. @<Store the string's name@>;
  4294. @^commented-out code@>
  4295. This module marks this string as |macro_ilk|; the commented-out code
  4296. will give a warning message when overwriting a previously defined
  4297. macro.
  4298. @<Store the string's name@>=
  4299. begin
  4300.   trace
  4301.   trace_pr_token;
  4302.   trace_pr_ln (' is a database-defined macro');
  4303.   ecart@/
  4304. lower_case (buffer, buf_ptr1, token_len);       {ignore case differences}
  4305. cur_macro_loc := str_lookup(buffer,buf_ptr1,token_len,macro_ilk,do_insert);
  4306. ilk_info[cur_macro_loc] := hash_text[cur_macro_loc]; {default in case of error}
  4307.   if (hash_found) then                          {already seen macro}
  4308.       macro_name_warning ('having its definition overwritten');
  4309.   @}@/
  4310. This module skips over the |equals_sign|, reads and stores the list of
  4311. field tokens that defines this macro (compressing |white_space|), and
  4312. reads a |right_outer_delim|.
  4313. @<Scan the string's definition field@>=
  4314. begin
  4315. if (scan_char <> equals_sign) then
  4316.     bib_equals_sign_expected_err;
  4317. incr(buf_ptr2);                         {skip over the |equals_sign|}
  4318. eat_bib_white_and_eof_check;
  4319. store_field := true;
  4320. if (not scan_and_store_the_field_value_and_eat_white) then
  4321.     return;
  4322. if (scan_char <> right_outer_delim) then
  4323.     bib_err ('Missing "',xchr[right_outer_delim],'" in string command');
  4324. incr(buf_ptr2);                         {skip over the |right_outer_delim|}
  4325. @^kludge@>
  4326. The variables for the function
  4327. |scan_and_store_the_field_value_and_eat_white| must be global since
  4328. the functions it calls use them too.  The alias kludge helps make the
  4329. stack space not overflow on some machines.
  4330. @d field_vl_str == ex_buf       {aliases, used ``only'' for this function}
  4331. @d field_end == ex_buf_ptr      {the end marker for the field-value string}
  4332. @d field_start == ex_buf_xptr   {and the start marker}
  4333. @<Globals in the outer block@>=
  4334. @!bib_brace_level : integer;    {brace nesting depth (excluding |str_delim|s)}
  4335. @^gymnastics@>
  4336. Since the function |scan_and_store_the_field_value_and_eat_white|
  4337. calls several other yet-to-be-described functions (one directly and
  4338. two indirectly), we must perform some topological gymnastics.
  4339. @<Procedures and functions for input scanning@>=
  4340. @<The scanning function |compress_bib_white|@>@;
  4341. @<The scanning function |scan_balanced_braces|@>@;
  4342. @<The scanning function |scan_a_field_token_and_eat_white|@>
  4343. This function scans the list of field tokens that define the field
  4344. value string.  If |store_field| is |true| it accumulates (indirectly)
  4345. in |field_vl_str| the concatenation of all the field tokens,
  4346. compressing nonnull |white_space| to a single |space| and, if the
  4347. field value is for a field (rather than a string definition), removing
  4348. any leading or trailing |white_space|; when it's finished it puts the
  4349. string into the hash table.  It returns |false| if there was a serious
  4350. syntax error.
  4351. @<Procedures and functions for input scanning@>=
  4352. function scan_and_store_the_field_value_and_eat_white : boolean;
  4353. label exit;
  4354. begin
  4355. scan_and_store_the_field_value_and_eat_white := false;
  4356.                                         {now it's easy to exit if necessary}
  4357. field_end := 0;
  4358. if (not scan_a_field_token_and_eat_white) then
  4359.     return;
  4360. while (scan_char = concat_char) do      {scan remaining field tokens}
  4361.     begin
  4362.     incr(buf_ptr2);                     {skip over the |concat_char|}
  4363.     eat_bib_white_and_eof_check;
  4364.     if (not scan_a_field_token_and_eat_white) then
  4365.         return;
  4366.     end;
  4367. if (store_field) then
  4368.     @<Store the field value string@>;
  4369. scan_and_store_the_field_value_and_eat_white := true;
  4370. exit:
  4371. Each field token is either a nonnegative number, a macro name (like
  4372. `jan'), or a brace-balanced string delimited by either |double_quote|s
  4373. or braces.  Thus there are four possibilities for the first character
  4374. of the field token: If it's a |left_brace| or a |double_quote|, the
  4375. token (with balanced braces, up to the matching |right_str_delim|) is
  4376. a string; if it's |numeric|, the token is a number; if it's anything
  4377. else, the token is a macro name (and should thus have been defined by
  4378. either the \.{.bst}-file's \.{macro} command or the \.{.bib}-file's
  4379. \.{string} command).  This function returns |false| if there was a
  4380. serious syntax error.
  4381. @<The scanning function |scan_a_field_token_and_eat_white|@>=
  4382. function scan_a_field_token_and_eat_white : boolean;
  4383. label exit;
  4384. begin
  4385. scan_a_field_token_and_eat_white := false; {now it's easy to exit if necessary}
  4386. case (scan_char) of
  4387.     left_brace :
  4388.         begin
  4389.         right_str_delim := right_brace;
  4390.         if (not scan_balanced_braces) then
  4391.             return;
  4392.         end;
  4393.     double_quote :
  4394.         begin
  4395.         right_str_delim := double_quote;
  4396.         if (not scan_balanced_braces) then
  4397.             return;
  4398.         end;
  4399.     "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" :
  4400.         @<Scan a number@>;
  4401.     othercases
  4402.         @<Scan a macro name@>
  4403. endcases;
  4404. eat_bib_white_and_eof_check;
  4405. scan_a_field_token_and_eat_white := true;
  4406. exit:
  4407. Now we come to the stuff that actually accumulates the field value to
  4408. be stored.  This module copies a character into |field_vl_str| if it
  4409. will fit; since it's so low level, it's implemented as a macro.
  4410. @d copy_char(#) == begin
  4411.                    if (field_end = buf_size) then
  4412.                        bib_field_too_long_err
  4413.                      else
  4414.                        begin
  4415.                        field_vl_str[field_end] := #;
  4416.                        incr(field_end);
  4417.                        end;
  4418.                    end
  4419. The \.{.bib}-specific scanning function |compress_bib_white| skips
  4420. over |white_space| characters within a string until hitting a nonwhite
  4421. character; in fact, it does everything |eat_bib_white_space| does, but
  4422. it also adds a |space| to |field_vl_str|.  This function is never
  4423. called if there are no |white_space| characters (or ends-of-line) to
  4424. be scanned (though the associated macro might be).  The function
  4425. returns |false| if there is a serious syntax error.
  4426. @d check_for_and_compress_bib_white_space ==
  4427.         begin
  4428.         if ((lex_class[scan_char]=white_space) or (buf_ptr2=last)) then
  4429.             if (not compress_bib_white) then
  4430.                 return;
  4431.         end
  4432. @<The scanning function |compress_bib_white|@>=
  4433. function compress_bib_white : boolean;
  4434. label exit;
  4435. begin
  4436. compress_bib_white := false;            {now it's easy to exit if necessary}
  4437. copy_char (space);
  4438. while (not scan_white_space) do         {no characters left; read another line}
  4439.     begin
  4440.     if (not input_ln(cur_bib_file)) then        {end-of-file; complain}
  4441.         begin
  4442.         eat_bib_print;
  4443.         return;
  4444.         end;
  4445.     incr(bib_line_num);
  4446.     buf_ptr2 := 0;
  4447.     end;
  4448. compress_bib_white := true;
  4449. exit:
  4450. This \.{.bib}-specific function scans a string with balanced braces,
  4451. stopping just past the matching |right_str_delim|.  How much work it
  4452. does depends on whether |store_field = true|.  It returns |false| if
  4453. there was a serious syntax error.
  4454. @<The scanning function |scan_balanced_braces|@>=
  4455. function scan_balanced_braces : boolean;
  4456. label loop_exit,@!exit;
  4457. begin
  4458. scan_balanced_braces := false;          {now it's easy to exit if necessary}
  4459. incr(buf_ptr2);                         {skip over the left-delimiter}
  4460. check_for_and_compress_bib_white_space;
  4461. if (field_end > 1) then
  4462.   if (field_vl_str[field_end-1] = space) then
  4463.     if (field_vl_str[field_end-2] = space) then {remove wrongly added |space|}
  4464.         decr(field_end);
  4465. bib_brace_level := 0;           {and we're at a non|white_space| character}
  4466. if (store_field) then
  4467.     @<Do a full brace-balanced scan@>
  4468.   else
  4469.     @<Do a quick brace-balanced scan@>;
  4470. incr(buf_ptr2);                         {skip over the |right_str_delim|}
  4471. scan_balanced_braces := true;
  4472. exit:
  4473. This module scans over a brace-balanced string without keeping track
  4474. of anything but the brace level.  It starts with |bib_brace_level = 0|
  4475. and at a non|white_space| character.
  4476. @<Do a quick brace-balanced scan@>=
  4477. begin
  4478. while (scan_char <> right_str_delim) do {we're at |bib_brace_level = 0|}
  4479.     if (scan_char = left_brace) then
  4480.         begin
  4481.         incr(bib_brace_level);
  4482.         incr(buf_ptr2);                 {skip over the |left_brace|}
  4483.         eat_bib_white_and_eof_check;
  4484.         while (bib_brace_level > 0) do
  4485.             @<Do a quick scan with |bib_brace_level > 0|@>;
  4486.         end
  4487.     else if (scan_char = right_brace) then
  4488.         bib_unbalanced_braces_err
  4489.     else
  4490.         begin
  4491.         incr(buf_ptr2);                 {skip over some other character}
  4492.         if (not scan3 (right_str_delim, left_brace, right_brace)) then
  4493.             eat_bib_white_and_eof_check;
  4494.         end
  4495. This module does the same as above but, because |bib_brace_level > 0|, it
  4496. doesn't have to look for a |right_str_delim|.
  4497. @<Do a quick scan with |bib_brace_level > 0|@>=
  4498. begin   {top part of the |while| loop---we're always at a nonwhite character}
  4499. if (scan_char = right_brace) then
  4500.     begin
  4501.     decr(bib_brace_level);
  4502.     incr(buf_ptr2);                     {skip over the |right_brace|}
  4503.     eat_bib_white_and_eof_check;
  4504.     end
  4505. else if (scan_char = left_brace) then
  4506.     begin
  4507.     incr(bib_brace_level);
  4508.     incr(buf_ptr2);                     {skip over the |left_brace|}
  4509.     eat_bib_white_and_eof_check;
  4510.     end
  4511.     begin
  4512.     incr(buf_ptr2);                     {skip over some other character}
  4513.     if (not scan2 (right_brace, left_brace)) then
  4514.         eat_bib_white_and_eof_check;
  4515.     end
  4516. This module scans over a brace-balanced string, compressing multiple
  4517. |white_space| characters into a single |space|.  It starts with
  4518. |bib_brace_level = 0| and starts at a non|white_space| character.
  4519. @<Do a full brace-balanced scan@>=
  4520. begin
  4521. while (scan_char <> right_str_delim) do
  4522.   case (scan_char) of
  4523.     left_brace :
  4524.         begin
  4525.         incr(bib_brace_level);
  4526.         copy_char (left_brace);@/
  4527.         incr(buf_ptr2);                 {skip over the |left_brace|}
  4528.         check_for_and_compress_bib_white_space;@/
  4529.         @<Do a full scan with |bib_brace_level > 0|@>;
  4530.         end;
  4531.     right_brace :
  4532.         bib_unbalanced_braces_err;
  4533.     othercases
  4534.         begin
  4535.         copy_char (scan_char);
  4536.         incr(buf_ptr2);                 {skip over some other character}
  4537.         check_for_and_compress_bib_white_space;
  4538.         end
  4539.   endcases;
  4540. This module is similar to the last but starts with |bib_brace_level > 0|
  4541. (and, like the last, it starts at a non|white_space| character).
  4542. @<Do a full scan with |bib_brace_level > 0|@>=
  4543. begin
  4544.   case (scan_char) of
  4545.     right_brace :
  4546.         begin
  4547.         decr(bib_brace_level);
  4548.         copy_char (right_brace);@/
  4549.         incr(buf_ptr2);                 {skip over the |right_brace|}
  4550.         check_for_and_compress_bib_white_space;
  4551.         if (bib_brace_level = 0) then
  4552.             goto loop_exit;
  4553.         end;
  4554.     left_brace :
  4555.         begin
  4556.         incr(bib_brace_level);
  4557.         copy_char (left_brace);@/
  4558.         incr(buf_ptr2);                 {skip over the |left_brace|}
  4559.         check_for_and_compress_bib_white_space;
  4560.         end;
  4561.     othercases
  4562.         begin
  4563.         copy_char (scan_char);
  4564.         incr(buf_ptr2);                 {skip over some other character}
  4565.         check_for_and_compress_bib_white_space;
  4566.         end
  4567.   endcases;
  4568. loop_exit:
  4569. @:this can't happen}{\quad A digit disappeared@>
  4570. This module scans a nonnegative number and copies it to |field_vl_str|
  4571. if it's to store the field.
  4572. @<Scan a number@>=
  4573. begin
  4574. if (not scan_nonneg_integer) then
  4575.     confusion ('A digit disappeared');
  4576. if (store_field) then
  4577.     begin
  4578.     tmp_ptr := buf_ptr1;
  4579.     while (tmp_ptr < buf_ptr2) do
  4580.         begin
  4581.         copy_char (buffer[tmp_ptr]);
  4582.         incr(tmp_ptr);
  4583.         end;
  4584.     end;
  4585. This module scans a macro name and copies its string to |field_vl_str|
  4586. if it's to store the field, complaining if the macro is recursive or
  4587. undefined.
  4588. @<Scan a macro name@>=
  4589. begin
  4590. scan_identifier (comma,right_outer_delim,concat_char);
  4591. bib_identifier_scan_check ('a field part');
  4592. if (store_field) then
  4593.     begin
  4594.     lower_case (buffer, buf_ptr1, token_len);   {ignore case differences}
  4595.     macro_name_loc := str_lookup(
  4596.                         buffer,buf_ptr1,token_len,macro_ilk,dont_insert);
  4597.     store_token := true;
  4598.     if (at_bib_command) then
  4599.       if (command_num = n_bib_string) then
  4600.         if (macro_name_loc = cur_macro_loc) then
  4601.             begin
  4602.             store_token := false;
  4603.             macro_name_warning ('used in its own definition');
  4604.             end;
  4605.     if (not hash_found) then
  4606.         begin
  4607.         store_token := false;
  4608.         macro_name_warning ('undefined');
  4609.         end;
  4610.     if (store_token) then
  4611.         @<Copy the macro string to |field_vl_str|@>;
  4612.     end;
  4613. The macro definition may have |white_space| that needs compressing,
  4614. because it may have been defined in the \.{.bst} file.
  4615. @<Copy the macro string to |field_vl_str|@>=
  4616. begin
  4617. tmp_ptr := str_start[ilk_info[macro_name_loc]];
  4618. tmp_end_ptr := str_start[ilk_info[macro_name_loc]+1];
  4619. if (field_end = 0) then
  4620.   if ((lex_class[str_pool[tmp_ptr]] = white_space) and (tmp_ptr < tmp_end_ptr))
  4621.                                                                         then
  4622.     begin               {compress leading |white_space| of first nonnull token}
  4623.     copy_char (space);
  4624.     incr(tmp_ptr);
  4625.     while ((lex_class[str_pool[tmp_ptr]] = white_space) and
  4626.                                                 (tmp_ptr <  tmp_end_ptr)) do
  4627.         incr(tmp_ptr);
  4628.     end;                {the next remaining character is non|white_space|}
  4629. while (tmp_ptr < tmp_end_ptr) do
  4630.     begin
  4631.     if (lex_class[str_pool[tmp_ptr]] <> white_space) then
  4632.         copy_char (str_pool[tmp_ptr])
  4633.       else if (field_vl_str[field_end-1] <> space) then
  4634.         copy_char (space);
  4635.     incr(tmp_ptr);
  4636.     end;
  4637. @^ham and eggs@>
  4638. Now it's time to store the field value in the hash table, and store an
  4639. appropriate pointer to it (depending on whether it's for a database
  4640. entry or command).  But first, if necessary, we remove a trailing
  4641. |space| and a leading |space| if these exist.  (Hey, if we had some
  4642. ham we could make ham-and-eggs if we had some eggs.)
  4643. @<Store the field value string@>=
  4644. begin
  4645. if (not at_bib_command) then            {chop trailing |space| for a field}
  4646.   if (field_end > 0) then
  4647.     if (field_vl_str[field_end-1] = space) then
  4648.         decr(field_end);
  4649. if ((not at_bib_command) and (field_vl_str[0] = space) and (field_end > 0))
  4650.                                 then    {chop leading |space| for a field}
  4651.     field_start := 1
  4652.   else
  4653.     field_start := 0;
  4654. field_val_loc := str_lookup(field_vl_str,field_start,field_end-field_start,
  4655.                                                         text_ilk,do_insert);
  4656. fn_type[field_val_loc] := str_literal;          {set the |fn_class|}
  4657.   trace
  4658.   trace_pr ('"');
  4659.   trace_pr_pool_str (hash_text[field_val_loc]);
  4660.   trace_pr_ln ('" is a field value');
  4661.   ecart@/
  4662. if (at_bib_command) then        {for a \.{preamble} or \.{string} command}
  4663.     @<Store the field value for a command@>
  4664.   else                                                  {for a database entry}
  4665.     @<Store the field value for a database entry@>;
  4666. @:this can't happen}{\quad Unknown database-file command@>
  4667. Here's where we store the goods when we're dealing with a command
  4668. rather than an entry.
  4669. @<Store the field value for a command@>=
  4670. begin
  4671. case (command_num) of
  4672.     n_bib_preamble :
  4673.         begin
  4674.         s_preamble[preamble_ptr] := hash_text[field_val_loc];
  4675.         incr(preamble_ptr);
  4676.         end;
  4677.     n_bib_string :
  4678.         ilk_info[cur_macro_loc] := hash_text[field_val_loc];
  4679.     othercases bib_cmd_confusion
  4680. endcases;
  4681. And here, an entry.
  4682. @<Store the field value for a database entry@>=
  4683. begin
  4684. field_ptr := entry_cite_ptr * num_fields + fn_info[field_name_loc];
  4685. if (field_info[field_ptr] <> missing) then
  4686.     begin
  4687.     print ('Warning--I''m ignoring ');
  4688.     print_pool_str (cite_list[entry_cite_ptr]);
  4689.     print ('''s extra "');
  4690.     print_pool_str (hash_text[field_name_loc]);
  4691.     bib_warn_newline ('" field');
  4692.     end
  4693.   else
  4694.     begin                       {the field was empty, store its new value}
  4695.     field_info[field_ptr] := hash_text[field_val_loc];
  4696.     if ((fn_info[field_name_loc] = crossref_num) and (not all_entries)) then
  4697.         @<Add or update a cross reference on |cite_list| if necessary@>;
  4698.     end;
  4699. @^kludge@>
  4700. @:this can't happen}{\quad Cite hash error@>
  4701. If the cross-referenced entry isn't already on |cite_list| we add it
  4702. (at least temporarily); if it is already on |cite_list| we update the
  4703. cross-reference count, if necessary.  Note that |all_entries| is
  4704. |false| here.  The alias kludge helps make the stack space not
  4705. overflow on some machines.
  4706. @d extra_buf == out_buf         {an alias, used only in this module}
  4707. @<Add or update a cross reference on |cite_list| if necessary@>=
  4708. begin
  4709. tmp_ptr := field_start;
  4710. while (tmp_ptr < field_end) do
  4711.     begin
  4712.     extra_buf[tmp_ptr] := field_vl_str[tmp_ptr];
  4713.     incr(tmp_ptr);
  4714.     end;
  4715. lower_case (extra_buf, field_start, field_end-field_start);
  4716.                                                 {convert to `canonical' form}
  4717. lc_cite_loc := str_lookup(extra_buf,field_start,field_end-field_start,
  4718.                                                         lc_cite_ilk,do_insert);
  4719. if (hash_found) then
  4720.     begin
  4721.     cite_loc := ilk_info[lc_cite_loc];  {even if there's a case mismatch}
  4722.     if (ilk_info[cite_loc] >= old_num_cites) then  {a previous \.{crossref}}
  4723.         incr(cite_info[ilk_info[cite_loc]]);
  4724.     end
  4725.   else
  4726.     begin                                       {it's a new \.{crossref}}
  4727.     cite_loc := str_lookup(field_vl_str,field_start,field_end-field_start,
  4728.                                                         cite_ilk,do_insert);
  4729.     if (hash_found) then
  4730.         hash_cite_confusion;
  4731.     add_database_cite (cite_ptr);               {this increments |cite_ptr|}
  4732.     cite_info[ilk_info[cite_loc]] := 1; {the first cross-ref for this cite key}
  4733.     end;
  4734. This procedure adds (or restores) to |cite_list| a cite key; it is
  4735. called only when |all_entries| is |true| or when adding
  4736. cross~references, and it assumes that |cite_loc| and |lc_cite_loc| are
  4737. set.  It also increments its argument.
  4738. @<Procedures and functions for handling numbers, characters, and strings@>=
  4739. procedure add_database_cite (var new_cite : cite_number);
  4740. begin
  4741. check_cite_overflow (new_cite);                 {make sure this cite will fit}
  4742. check_field_overflow (num_fields*new_cite);
  4743. cite_list[new_cite] := hash_text[cite_loc];
  4744. ilk_info[cite_loc] := new_cite;
  4745. ilk_info[lc_cite_loc] := cite_loc;
  4746. incr(new_cite);
  4747. And now, back to processing an entry (rather than a command).  This
  4748. module reads a left outer-delimiter and a database key.
  4749. @<Scan the entry's database key@>=
  4750. begin
  4751. if (scan_char = left_brace) then
  4752.     right_outer_delim := right_brace
  4753. else if (scan_char = left_paren) then
  4754.     right_outer_delim := right_paren
  4755.     bib_one_of_two_expected_err (left_brace,left_paren);
  4756. incr(buf_ptr2);                                 {skip over the left-delimiter}
  4757. eat_bib_white_and_eof_check;
  4758. if (right_outer_delim = right_paren) then       {to allow it in a database key}
  4759.     begin
  4760.     if (scan1_white(comma)) then                {ok if database key ends line}
  4761.         do_nothing;
  4762.     end
  4763.   else
  4764.     if (scan2_white(comma,right_brace)) then {|right_brace=right_outer_delim|}
  4765.         do_nothing;
  4766. @<Check for a database key of interest@>;
  4767. @^kludge@>
  4768. The lower-case version of this database key must correspond to one in
  4769. |cite_list|, or else |all_entries| must be |true|, if this entry is to
  4770. be included in the reference list.  Accordingly, this module sets
  4771. |store_entry|, which determines whether the relevant information for
  4772. this entry is stored.  The alias kludge helps make the stack space not
  4773. overflow on some machines.
  4774. @d ex_buf3 == ex_buf            {an alias, used only in this module}
  4775. @<Check for a database key of interest@>=
  4776. begin
  4777.   trace
  4778.   trace_pr_token;
  4779.   trace_pr_ln (' is a database key');
  4780.   ecart@/
  4781. tmp_ptr := buf_ptr1;
  4782. while (tmp_ptr < buf_ptr2) do
  4783.     begin
  4784.     ex_buf3[tmp_ptr] := buffer[tmp_ptr];
  4785.     incr(tmp_ptr);
  4786.     end;
  4787. lower_case (ex_buf3, buf_ptr1, token_len);      {convert to `canonical' form}
  4788. if (all_entries) then
  4789.     lc_cite_loc := str_lookup(ex_buf3,buf_ptr1,token_len,lc_cite_ilk,do_insert)
  4790.   else
  4791.     lc_cite_loc := str_lookup(ex_buf3,buf_ptr1,token_len,lc_cite_ilk,
  4792.                                                                 dont_insert);
  4793. if (hash_found) then
  4794.     begin
  4795.     entry_cite_ptr := ilk_info[ilk_info[lc_cite_loc]];
  4796.     @<Check for a duplicate or \.{crossref}-matching database key@>;
  4797.     end;
  4798. store_entry := true;    {unless |(not hash_found) and (not all_entries)|}
  4799. if (all_entries) then
  4800.     @<Put this cite key in its place@>
  4801.   else if (not hash_found) then
  4802.     store_entry := false;       {no such cite key exists on |cite_list|}
  4803. if (store_entry) then
  4804.     @<Make sure this entry is ok before proceeding@>;
  4805. @:this can't happen}{\quad The cite list is messed up@>
  4806. It's illegal to have two (or more) entries with the same database key
  4807. (even if there are case differrences), and we skip the rest of the
  4808. entry for such a repeat occurrence.  Also, we make this entry's
  4809. database key the official |cite_list| key if it's on |cite_list| only
  4810. because of cross references.
  4811. @<Check for a duplicate or \.{crossref}-matching database key@>=
  4812. begin
  4813. if ((not all_entries) or (entry_cite_ptr < all_marker)
  4814.                                 or (entry_cite_ptr >= old_num_cites)) then
  4815.     begin
  4816.     if (type_list[entry_cite_ptr] = empty) then
  4817.         begin
  4818.         @<Make sure this entry's database key is on |cite_list|@>;
  4819.         goto first_time_entry;
  4820.         end;
  4821.     end
  4822. else if (not entry_exists[entry_cite_ptr]) then
  4823.     begin
  4824.     @<Find the lower-case equivalent of the |cite_info| key@>;
  4825.     if (lc_xcite_loc = lc_cite_loc) then
  4826.         goto first_time_entry;
  4827.     end;@/
  4828.                                 {oops---repeated entry---issue a reprimand}
  4829. if (type_list[entry_cite_ptr] = empty) then
  4830.     confusion ('The cite list is messed up');
  4831. bib_err ('Repeated entry');
  4832. first_time_entry:  {note that when we leave normally, |hash_found| is |true|}
  4833. An entry that's on |cite_list| only because of cross referencing must
  4834. have its database key (rather than one of the \.{crossref} keys) as
  4835. the official |cite_list| string.  Here's where we assure that.  The
  4836. variable |hash_found| is |true| upon entrance to and exit from this
  4837. module.
  4838. @<Make sure this entry's database key is on |cite_list|@>=
  4839. begin
  4840. if ((not all_entries) and (entry_cite_ptr >= old_num_cites)) then
  4841.     begin
  4842.     cite_loc := str_lookup(buffer,buf_ptr1,token_len,cite_ilk,do_insert);
  4843.     if (not hash_found) then
  4844.         begin                   {it's not on |cite_list|---put it there}
  4845.         ilk_info[lc_cite_loc] := cite_loc;
  4846.         ilk_info[cite_loc] := entry_cite_ptr;
  4847.         cite_list[entry_cite_ptr] := hash_text[cite_loc];@/
  4848.         hash_found := true;             {restore this value for later use}
  4849.         end;
  4850.     end;
  4851. @^kludge@>
  4852. @:this can't happen}{\quad A cite key disappeared@>
  4853. This module, a simpler version of the
  4854. |find_cite_locs_for_this_cite_key| function, exists primarily to
  4855. compute |lc_xcite_loc|.  When this code is executed we have
  4856. |(all_entries) and (entry_cite_ptr >= all_marker) and (not
  4857. entry_exists[entry_cite_ptr])|.  The alias kludge helps make the stack
  4858. space not overflow on some machines.
  4859. @d ex_buf4 == ex_buf            {aliases, used only}
  4860. @d ex_buf4_ptr == ex_buf_ptr    {in this module}
  4861. @<Find the lower-case equivalent of the |cite_info| key@>=
  4862. begin
  4863. ex_buf4_ptr := 0;
  4864. tmp_ptr := str_start[cite_info[entry_cite_ptr]];
  4865. tmp_end_ptr := str_start[cite_info[entry_cite_ptr]+1];
  4866. while (tmp_ptr < tmp_end_ptr) do
  4867.     begin
  4868.     ex_buf4[ex_buf4_ptr] := str_pool[tmp_ptr];
  4869.     incr(ex_buf4_ptr);
  4870.     incr(tmp_ptr);
  4871.     end;
  4872. lower_case (ex_buf4, 0, length(cite_info[entry_cite_ptr]));
  4873.                                                 {convert to `canonical' form}
  4874. lc_xcite_loc := str_lookup(ex_buf4,0,length(cite_info[entry_cite_ptr]),
  4875.                                                 lc_cite_ilk,dont_insert);
  4876. if (not hash_found) then
  4877.     cite_key_disappeared_confusion;
  4878. @:this can't happen}{\quad A cite key disappeared@>
  4879. Here's another bug complaint.
  4880. @<Procedures and functions for all file I/O, error messages, and such@>=
  4881. procedure cite_key_disappeared_confusion;
  4882. begin
  4883. confusion ('A cite key disappeared');
  4884. @:this can't happen}{\quad Cite hash error@>
  4885. This module, which gets executed only when |all_entries| is |true|,
  4886. does one of three things, depending on whether or not, and where, the
  4887. cite key appears on |cite_list|: If it's on |cite_list| before
  4888. |all_marker|, there's nothing to be done; if it's after |all_marker|,
  4889. it must be reinserted (at the current place) and we must note that its
  4890. corresponding entry exists; and if it's not on |cite_list| at all, it
  4891. must be inserted for the first time.  The |goto| construct must stay
  4892. as is, partly because some \PASCAL\ compilers might complain if
  4893. ``|and|'' were to connect the two boolean expressions (since
  4894. |entry_cite_ptr| could be uninitialized when |hash_found| is |false|).
  4895. @<Put this cite key in its place@>=
  4896. begin
  4897. if (hash_found) then
  4898.     begin
  4899.     if (entry_cite_ptr < all_marker) then
  4900.         goto cite_already_set           {that is, do nothing}
  4901.       else
  4902.         begin
  4903.         entry_exists[entry_cite_ptr] := true;
  4904.         cite_loc := ilk_info[lc_cite_loc];
  4905.         end;
  4906.     end
  4907.   else
  4908.     begin                               {this is a new key}
  4909.     cite_loc := str_lookup(buffer,buf_ptr1,token_len,cite_ilk,do_insert);
  4910.     if (hash_found) then
  4911.         hash_cite_confusion;
  4912.     end;@/
  4913. entry_cite_ptr := cite_ptr;
  4914. add_database_cite (cite_ptr);           {this increments |cite_ptr|}
  4915. cite_already_set:
  4916. @^case mismatch errors@>
  4917. @^commented-out code@>
  4918. We must give a warning if this entry~type doesn't exist.  Also, we
  4919. point the appropriate entry of |type_list| to the entry type just read
  4920. above.
  4921. For SCRIBE compatibility, the code to give a warning for a case
  4922. mismatch between a cite key and a database key has been commented out.
  4923. In fact, SCRIBE is the reason that it doesn't produce an error message
  4924. outright.  (Note: Case mismatches between two cite keys produce
  4925. full-blown errors.)
  4926. @<Make sure this entry is ok before proceeding@>=
  4927. begin
  4928.   dummy_loc := str_lookup(buffer,buf_ptr1,token_len,cite_ilk,dont_insert);
  4929.   if (not hash_found) then      {give a warning if there is a case difference}
  4930.     begin
  4931.     print ('Warning--case mismatch, database key "');
  4932.     print_token;
  4933.     print ('", cite key "');
  4934.     print_pool_str (cite_list[entry_cite_ptr]);
  4935.     bib_warn_newline ('"');
  4936.     end;
  4937.   @}@/
  4938. if (type_exists) then
  4939.     type_list[entry_cite_ptr] := entry_type_loc
  4940.   else
  4941.     begin
  4942.     type_list[entry_cite_ptr] := undefined;
  4943.     print ('Warning--entry type for "');
  4944.     print_token;
  4945.     bib_warn_newline ('" isn''t style-file defined');
  4946.     end;
  4947. This module reads a |comma| and a field as many times as it can, and
  4948. then reads a |right_outer_delim|, ending the current entry.
  4949. @<Scan the entry's list of fields@>=
  4950. begin
  4951. while (scan_char <> right_outer_delim) do
  4952.     begin
  4953.     if (scan_char <> comma) then
  4954.         bib_one_of_two_expected_err (comma,right_outer_delim);
  4955.     incr(buf_ptr2);                     {skip over the |comma|}
  4956.     eat_bib_white_and_eof_check;
  4957.     if (scan_char = right_outer_delim) then
  4958.         goto loop_exit;
  4959.     @<Get the next field name@>;
  4960.     eat_bib_white_and_eof_check;
  4961.     if (not scan_and_store_the_field_value_and_eat_white) then
  4962.         return;
  4963.     end;
  4964. loop_exit:
  4965. incr(buf_ptr2);                         {skip over the |right_outer_delim|}
  4966. This module reads a field name; its contents won't be stored unless it
  4967. was declared in the \.{.bst} file and |store_entry = true|.
  4968. @<Get the next field name@>=
  4969. begin
  4970. scan_identifier (equals_sign,equals_sign,equals_sign);
  4971. bib_identifier_scan_check ('a field name');
  4972.   trace
  4973.   trace_pr_token;
  4974.   trace_pr_ln (' is a field name');
  4975.   ecart@/
  4976. store_field := false;
  4977. if (store_entry) then
  4978.     begin
  4979.     lower_case (buffer, buf_ptr1, token_len);   {ignore case differences}
  4980.     field_name_loc := str_lookup(
  4981.                         buffer,buf_ptr1,token_len,bst_fn_ilk,dont_insert);
  4982.     if (hash_found) then
  4983.       if (fn_type[field_name_loc]=field) then@/
  4984.         store_field := true;  {field name was pre-defined or \.{.bst}-declared}
  4985.     end;
  4986. eat_bib_white_and_eof_check;
  4987. if (scan_char <> equals_sign) then
  4988.     bib_equals_sign_expected_err;
  4989. incr(buf_ptr2);                 {skip over the |equals_sign|}
  4990. This gets things ready for further \.{.bst} processing.
  4991. @<Final initialization for processing the entries@>=
  4992. begin
  4993. num_cites := cite_ptr;  {to include database and \.{crossref} cite keys, too}
  4994. num_preamble_strings := preamble_ptr;   {number of \.{preamble} commands seen}
  4995. @<Add cross-reference information@>;
  4996. @<Subtract cross-reference information@>;
  4997. @<Remove missing entries or those cross referenced too few times@>;
  4998. @<Initialize the |int_entry_var|s@>;
  4999. @<Initialize the |str_entry_var|s@>;
  5000. @<Initialize the |sorted_cites|@>;
  5001. @^child entry@>
  5002. @^cross references@>
  5003. @^nested cross references@>
  5004. @^parent entry@>
  5005. Now we update any entry (here called a {\it child\/} entry) that
  5006. cross~referenced another (here called a {\it parent\/} entry); this
  5007. cross~referencing occurs when the child's \.{crossref} field (value)
  5008. consists of the parent's database key.  To do the update, we replace
  5009. the child's |missing| fields by the corresponding fields of the
  5010. parent.  Also, we make sure the \.{crossref} field contains the
  5011. case-correct version.  Finally, although it is technically illegal to
  5012. nest cross~references, and although we give a warning (a few modules
  5013. hence) when someone tries, we do what we can to accommodate the
  5014. attempt.
  5015. @<Add cross-reference information@>=
  5016. begin
  5017. cite_ptr := 0;
  5018. while (cite_ptr < num_cites) do
  5019.     begin
  5020.     field_ptr := cite_ptr * num_fields + crossref_num;
  5021.     if (field_info[field_ptr] <> missing) then
  5022.       if (find_cite_locs_for_this_cite_key (field_info[field_ptr])) then
  5023.         begin
  5024.         cite_loc := ilk_info[lc_cite_loc];
  5025.         field_info[field_ptr] := hash_text[cite_loc];
  5026.         cite_parent_ptr := ilk_info[cite_loc];
  5027.         field_ptr := cite_ptr * num_fields + num_pre_defined_fields;
  5028.         field_end_ptr := field_ptr - num_pre_defined_fields + num_fields;
  5029.         field_parent_ptr := cite_parent_ptr * num_fields
  5030.                                                 + num_pre_defined_fields;
  5031.         while (field_ptr < field_end_ptr) do
  5032.             begin
  5033.             if (field_info[field_ptr] = missing) then
  5034.                 field_info[field_ptr] := field_info[field_parent_ptr];
  5035.             incr(field_ptr);
  5036.             incr(field_parent_ptr);
  5037.             end;
  5038.         end;
  5039.     incr(cite_ptr);
  5040.     end;
  5041. @^kludge@>
  5042. @^raisin@>
  5043. Occasionally we need to figure out the hash-table location of a given
  5044. cite-key string and its lower-case equivalent.  This function does
  5045. that.  To perform the task it needs to borrow a buffer, a need that
  5046. gives rise to the alias kludge---it helps make the stack space not
  5047. overflow on some machines (and while it's at it, it'll borrow a
  5048. pointer, too).  Finally, the function returns |true| if the cite key
  5049. exists on |cite_list|, and its sets |cite_hash_found| according to
  5050. whether or not it found the actual version (before |lower_case|ing) of
  5051. the cite key; however, its {\sl raison d'\^$\mkern-8mu$etre\/}
  5052. (literally, ``to eat a raisin'') is to compute |cite_loc| and
  5053. |lc_cite_loc|.
  5054. @d ex_buf5 == ex_buf            {aliases, used only}
  5055. @d ex_buf5_ptr == ex_buf_ptr    {in this module}
  5056. @<Procedures and functions for handling numbers, characters, and strings@>=
  5057. function find_cite_locs_for_this_cite_key (@!cite_str : str_number) : boolean;
  5058. begin
  5059. ex_buf5_ptr := 0;
  5060. tmp_ptr := str_start[cite_str];
  5061. tmp_end_ptr := str_start[cite_str+1];
  5062. while (tmp_ptr < tmp_end_ptr) do
  5063.     begin
  5064.     ex_buf5[ex_buf5_ptr] := str_pool[tmp_ptr];
  5065.     incr(ex_buf5_ptr);
  5066.     incr(tmp_ptr);
  5067.     end;
  5068. cite_loc := str_lookup(ex_buf5,0,length(cite_str),cite_ilk,dont_insert);
  5069. cite_hash_found := hash_found;
  5070. lower_case (ex_buf5, 0, length(cite_str));      {convert to `canonical' form}
  5071. lc_cite_loc := str_lookup(ex_buf5,0,length(cite_str),lc_cite_ilk,dont_insert);
  5072. if (hash_found) then
  5073.     find_cite_locs_for_this_cite_key := true
  5074.   else
  5075.     find_cite_locs_for_this_cite_key := false;
  5076. @:this can't happen}{\quad Cite hash error@>
  5077. Here we remove the \.{crossref} field value for each child whose
  5078. parent was cross~referenced too few times.  We also issue any
  5079. necessary warnings arising from a bad cross~reference.
  5080. @<Subtract cross-reference information@>=
  5081. begin
  5082. cite_ptr := 0;
  5083. while (cite_ptr < num_cites) do
  5084.     begin
  5085.     field_ptr := cite_ptr * num_fields + crossref_num;
  5086.     if (field_info[field_ptr] <> missing) then
  5087.       if (not find_cite_locs_for_this_cite_key (field_info[field_ptr])) then
  5088.         begin                           {the parent is not on |cite_list|}
  5089.         if (cite_hash_found) then
  5090.             hash_cite_confusion;
  5091.         nonexistent_cross_reference_error;
  5092.         field_info[field_ptr] := missing;       {remove the \.{crossref} ptr}
  5093.         end
  5094.       else
  5095.         begin                           {the parent exists on |cite_list|}
  5096.         if (cite_loc <> ilk_info[lc_cite_loc]) then
  5097.             hash_cite_confusion;
  5098.         cite_parent_ptr := ilk_info[cite_loc];
  5099.         if (type_list[cite_parent_ptr] = empty) then
  5100.             begin
  5101.             nonexistent_cross_reference_error;@/
  5102.             field_info[field_ptr] := missing;   {remove the \.{crossref} ptr}
  5103.             end
  5104.           else
  5105.             begin                       {the parent exists in the database too}
  5106.             field_parent_ptr := cite_parent_ptr * num_fields + crossref_num;
  5107.             if (field_info[field_parent_ptr] <> missing) then
  5108.                 @<Complain about a nested cross reference@>;
  5109.             if ((not all_entries) and (cite_parent_ptr >= old_num_cites) and
  5110.                         (cite_info[cite_parent_ptr] < min_crossrefs)) then@/
  5111.                 field_info[field_ptr] := missing; {remove the \.{crossref} ptr}
  5112.             end;
  5113.         end;
  5114.     incr(cite_ptr);
  5115.     end;
  5116. This procedure exists to save space, since it's used twice---once for
  5117. each of the two succeeding modules.
  5118. @<Procedures and functions for all file I/O, error messages, and such@>=
  5119. procedure bad_cross_reference_print (@!s:str_number);
  5120. begin
  5121. print ('--entry "');
  5122. print_pool_str (cur_cite_str);
  5123. print_ln ('"');
  5124. print ('refers to entry "');
  5125. print_pool_str (s);
  5126. When an entry being cross referenced doesn't exist on |cite_list|, we
  5127. complain.
  5128. @<Procedures and functions for all file I/O, error messages, and such@>=
  5129. procedure nonexistent_cross_reference_error;
  5130. begin
  5131. print ('A bad cross reference-');
  5132. bad_cross_reference_print (field_info[field_ptr]);
  5133. print_ln ('", which doesn''t exist');
  5134. mark_error;
  5135. We also complain when an entry being cross referenced has a
  5136. non|missing| \.{crossref} field itself, but this one is just a
  5137. warning, not a full-blown error.
  5138. @<Complain about a nested cross reference@>=
  5139. begin
  5140. print ('Warning--you''ve nested cross references');
  5141. bad_cross_reference_print (cite_list[cite_parent_ptr]);
  5142. print_ln ('", which also refers to something');
  5143. mark_warning;
  5144. We remove (and give a warning for) each cite key on the original
  5145. |cite_list| without a corresponding database entry.  And we remove any
  5146. entry that was included on |cite_list| only because it was
  5147. cross~referenced, yet was cross~referenced fewer than |min_crossrefs|
  5148. times.  Throughout this module, |cite_ptr| points to the next cite key
  5149. to be checked and |cite_xptr| points to the next permanent spot on
  5150. |cite_list|.
  5151. @<Remove missing entries or those cross referenced too few times@>=
  5152. begin
  5153. cite_ptr := 0;
  5154. while (cite_ptr < num_cites) do
  5155.     begin
  5156.     if (type_list[cite_ptr] = empty) then
  5157.         print_missing_entry (cur_cite_str)
  5158.     else if ((all_entries) or (cite_ptr < old_num_cites) or
  5159.                                 (cite_info[cite_ptr] >= min_crossrefs)) then
  5160.         begin
  5161.         if (cite_ptr > cite_xptr) then
  5162.             @<Slide this cite key down to its permanent spot@>;
  5163.         incr(cite_xptr);
  5164.         end;
  5165.     incr(cite_ptr);
  5166.     end;
  5167. num_cites := cite_xptr;
  5168. if (all_entries) then
  5169.     @<Complain about missing entries whose cite keys got overwritten@>;
  5170. When a cite key on the original |cite_list| (or added to |cite_list|
  5171. because of cross~referencing) didn't appear in the database, complain.
  5172. @<Procedures and functions for all file I/O, error messages, and such@>=
  5173. procedure print_missing_entry (@!s:str_number);
  5174. begin
  5175. print ('Warning--I didn''t find a database entry for "');
  5176. print_pool_str (s);
  5177. print_ln ('"');
  5178. mark_warning;
  5179. @:this can't happen}{\quad A cite key disappeared@>
  5180. @:this can't happen}{\quad Cite hash error@>
  5181. We have to move to its final resting place all the entry information
  5182. associated with the exact location in |cite_list| of this cite key.
  5183. @<Slide this cite key down to its permanent spot@>=
  5184. begin
  5185. cite_list[cite_xptr] := cite_list[cite_ptr];
  5186. type_list[cite_xptr] := type_list[cite_ptr];
  5187. if (not find_cite_locs_for_this_cite_key (cite_list[cite_ptr])) then
  5188.     cite_key_disappeared_confusion;
  5189. if ((not cite_hash_found) or (cite_loc <> ilk_info[lc_cite_loc])) then
  5190.     hash_cite_confusion;
  5191. ilk_info[cite_loc] := cite_xptr;@/
  5192. field_ptr := cite_xptr * num_fields;
  5193. field_end_ptr := field_ptr + num_fields;
  5194. tmp_ptr := cite_ptr * num_fields;
  5195. while (field_ptr < field_end_ptr) do
  5196.     begin
  5197.     field_info[field_ptr] := field_info[tmp_ptr];
  5198.     incr(field_ptr);
  5199.     incr(tmp_ptr);
  5200.     end;
  5201. We need this module only when we're including the whole database.
  5202. It's for missing entries whose cite key originally resided in
  5203. |cite_list| at a spot that another cite key (might have) claimed.
  5204. @<Complain about missing entries whose cite keys got overwritten@>=
  5205. begin
  5206. cite_ptr := all_marker;
  5207. while (cite_ptr < old_num_cites) do
  5208.     begin
  5209.     if (not entry_exists[cite_ptr]) then
  5210.         print_missing_entry (cite_info[cite_ptr]);
  5211.     incr(cite_ptr);
  5212.     end;
  5213. @:BibTeX capacity exceeded}{\quad total number of integer entry-variables@>
  5214. This module initializes all |int_entry_var|s of all entries to 0, the
  5215. value to which all integers are initialized.
  5216. @<Initialize the |int_entry_var|s@>=
  5217. begin
  5218. if (num_ent_ints*num_cites > max_ent_ints) then
  5219.     begin
  5220.     print (num_ent_ints*num_cites,': ');
  5221.     overflow('total number of integer entry-variables ',max_ent_ints);
  5222.     end;
  5223. int_ent_ptr := 0;
  5224. while (int_ent_ptr < num_ent_ints*num_cites) do
  5225.     begin
  5226.     entry_ints[int_ent_ptr] := 0;
  5227.     incr(int_ent_ptr);
  5228.     end;
  5229. @:BibTeX capacity exceeded}{\quad total number of string entry-variables@>
  5230. This module initializes all |str_entry_var|s of all entries to the
  5231. null string, the value to which all strings are initialized.
  5232. @<Initialize the |str_entry_var|s@>=
  5233. begin
  5234. if (num_ent_strs*num_cites > max_ent_strs) then
  5235.     begin
  5236.     print (num_ent_strs*num_cites,': ');
  5237.     overflow('total number of string entry-variables ',max_ent_strs);
  5238.     end;
  5239. str_ent_ptr := 0;
  5240. while (str_ent_ptr < num_ent_strs*num_cites) do
  5241.     begin
  5242.     entry_strs[str_ent_ptr][0] := end_of_string;
  5243.     incr(str_ent_ptr);
  5244.     end;
  5245. The array |sorted_cites| initially specifies that the entries are to
  5246. be processed in order of cite-key occurrence.  The \.{sort} command
  5247. may change this to whatever it likes (which, we hope, is whatever the
  5248. style-designer instructs it to like).  We make |sorted_cites| an alias
  5249. to save space; this works fine because we're done with |cite_info|.
  5250. @d sorted_cites == cite_info    {an alias used for the rest of the program}
  5251. @<Initialize the |sorted_cites|@>=
  5252. begin
  5253. cite_ptr := 0;
  5254. while (cite_ptr < num_cites) do
  5255.     begin
  5256.     sorted_cites[cite_ptr] := cite_ptr;
  5257.     incr(cite_ptr);
  5258.     end;
  5259. @* Executing the style file.
  5260. This part of the program produces the output by executing the
  5261. \.{.bst}-file commands \.{execute}, \.{iterate}, \.{reverse}, and
  5262. \.{sort}.  To do this it uses a stack (consisting of the two arrays
  5263. |lit_stack| and |lit_stk_type|) for storing literals, a buffer
  5264. |ex_buf| for manipulating strings, and an array |sorted_cites|
  5265. for holding pointers to the sorted cite keys (|sorted_cites| is an
  5266. alias of |cite_info|).
  5267. @<Globals in the outer block@>=
  5268. @!lit_stack : array[lit_stk_loc] of integer;    {the literal function stack}
  5269. @!lit_stk_type : array[lit_stk_loc] of stk_type; {their corresponding types}
  5270. @!lit_stk_ptr : lit_stk_loc;    {points just above the top of the stack}
  5271. @!cmd_str_ptr : str_number;     {stores value of |str_ptr| during execution}
  5272. @!ent_chr_ptr : 0..ent_str_size; {points at a |str_entry_var| character}
  5273. @!glob_chr_ptr : 0..glob_str_size; {points at a |str_global_var| character}
  5274. @!ex_buf : buf_type;            {a buffer for manipulating strings}
  5275. @!ex_buf_ptr : buf_pointer;     {general |ex_buf| location}
  5276. @!ex_buf_length : buf_pointer;  {the length of the current string in |ex_buf|}
  5277. @!out_buf : buf_type;           {the \.{.bbl} output buffer}
  5278. @!out_buf_ptr : buf_pointer;    {general |out_buf| location}
  5279. @!out_buf_length : buf_pointer; {the length of the current string in |out_buf|}
  5280. @!mess_with_entries : boolean;  {|true| if functions can use entry info}
  5281. @!sort_cite_ptr : cite_number;  {a loop index for the sorted cite keys}
  5282. @!sort_key_num : str_ent_loc;   {index for the |str_entry_var| \.{sort.key\$}}
  5283. @!brace_level : integer;        {the brace nesting depth within a string}
  5284. Where |lit_stk_loc| is a stack location, and where |stk_type| gives
  5285. one of the three types of literals (an integer, a string, or a
  5286. function) or a special marker.  If a |lit_stk_type| element is a
  5287. |stk_int| then the corresponding |lit_stack| element is an integer; if
  5288. a |stk_str|, then a pointer to a |str_pool| string; and if a |stk_fn|,
  5289. then a pointer to the function's hash-table location.  However, if the
  5290. literal should have been a |stk_str| that was the value of a field
  5291. that happened to be |missing|, then the special value
  5292. |stk_field_missing| goes on the stack instead; its corresponding
  5293. |lit_stack| element is a pointer to the field-name's string.  Finally,
  5294. |stk_empty| is the type of a literal popped from an empty stack.
  5295. @d stk_int = 0          {an integer literal}
  5296. @d stk_str = 1          {a string literal}
  5297. @d stk_fn = 2           {a function literal}
  5298. @d stk_field_missing = 3 {a special marker: a field value was missing}
  5299. @d stk_empty = 4        {another: the stack was empty when this was popped}
  5300. @d last_lit_type = 4    {the same number as on the line above}
  5301. @<Types in the outer block@>=
  5302. @!lit_stk_loc = 0..lit_stk_size;        {the stack range}
  5303. @!stk_type = 0..last_lit_type;          {the literal types}
  5304. And the first output line requires this initialization.
  5305. @<Set initial values of key variables@>=
  5306. out_buf_length := 0;
  5307. When there's an error while executing \.{.bst} functions, what we do
  5308. depends on whether the function is messing with the entries.
  5309. Furthermore this error is serious enough to classify as an
  5310. |error_message| instead of a |warning_message|.  These messages (that
  5311. is, from |bst_ex_warn|) are meant both for the user and for the style
  5312. designer while debugging.
  5313. @d bst_ex_warn(#) == begin              {error while executing some function}
  5314.                      print (#);
  5315.                      bst_ex_warn_print;
  5316.                      end
  5317. @<Procedures and functions for all file I/O, error messages, and such@>=
  5318. procedure bst_ex_warn_print;
  5319. begin
  5320. if (mess_with_entries) then
  5321.     begin
  5322.     print (' for entry ');
  5323.     print_pool_str (cur_cite_str);
  5324.     end;
  5325. print_newline;
  5326. print ('while executing-');
  5327. bst_ln_num_print;
  5328. mark_error;
  5329. When an error is so harmless, we print a |warning_message| instead of
  5330. an |error_message|.
  5331. @d bst_mild_ex_warn(#) == begin         {error while executing some function}
  5332.                           print (#);
  5333.                           bst_mild_ex_warn_print;
  5334.                           end
  5335. @<Procedures and functions for all file I/O, error messages, and such@>=
  5336. procedure bst_mild_ex_warn_print;
  5337. begin
  5338. if (mess_with_entries) then
  5339.     begin
  5340.     print (' for entry ');
  5341.     print_pool_str (cur_cite_str);
  5342.     end;
  5343. print_newline;
  5344. bst_warn ('while executing');                   {This does the |mark_warning|}
  5345. It's illegal to mess with the entry information at certain times;
  5346. here's a complaint for these times.
  5347. @<Procedures and functions for all file I/O, error messages, and such@>=
  5348. procedure bst_cant_mess_with_entries_print;
  5349. begin
  5350. bst_ex_warn ('You can''t mess with entries here');
  5351. This module executes a single specified function once.  It can't do
  5352. anything with the entries.
  5353. @<Perform an \.{execute} command@>=
  5354. begin
  5355. init_command_execution;
  5356. mess_with_entries := false;
  5357. execute_fn (fn_loc);
  5358. check_command_execution;
  5359. This module iterates a single specified function for all entries
  5360. specified by |cite_list|.
  5361. @<Perform an \.{iterate} command@>=
  5362. begin
  5363. init_command_execution;
  5364. mess_with_entries := true;
  5365. sort_cite_ptr := 0;
  5366. while (sort_cite_ptr < num_cites) do
  5367.     begin
  5368.     cite_ptr := sorted_cites[sort_cite_ptr];
  5369.       trace
  5370.       trace_pr_pool_str (hash_text[fn_loc]);
  5371.       trace_pr (' to be iterated on ');
  5372.       trace_pr_pool_str (cur_cite_str);
  5373.       trace_pr_newline;
  5374.       ecart@/
  5375.     execute_fn (fn_loc);
  5376.     check_command_execution;
  5377.     incr(sort_cite_ptr);
  5378.     end;
  5379. This module iterates a single specified function for all entries
  5380. specified by |cite_list|, but does it in reverse order.
  5381. @<Perform a \.{reverse} command@>=
  5382. begin
  5383. init_command_execution;
  5384. mess_with_entries := true;
  5385. if (num_cites > 0) then
  5386.     begin
  5387.     sort_cite_ptr := num_cites;
  5388.     repeat
  5389.         decr(sort_cite_ptr);
  5390.         cite_ptr := sorted_cites[sort_cite_ptr];
  5391.           trace
  5392.           trace_pr_pool_str (hash_text[fn_loc]);
  5393.           trace_pr (' to be iterated in reverse on ');
  5394.           trace_pr_pool_str (cur_cite_str);
  5395.           trace_pr_newline;
  5396.           ecart@/
  5397.         execute_fn (fn_loc);
  5398.         check_command_execution;
  5399.       until (sort_cite_ptr = 0);
  5400.     end;
  5401. This module sorts the entries based on \.{sort.key\$}; it is a stable
  5402. sort.
  5403. @<Perform a \.{sort} command@>=
  5404. begin
  5405.   trace
  5406.   trace_pr_ln ('Sorting the entries');
  5407.   ecart@/
  5408. if (num_cites > 1) then
  5409.     quick_sort (0, num_cites-1);
  5410.   trace
  5411.   trace_pr_ln ('Done sorting');
  5412.   ecart@/
  5413. These next two procedures (actually, one procedures and one function,
  5414. but who's counting) are subroutines for |quick_sort|, which follows.
  5415. The |swap| procedure exchanges the two elements its arguments point
  5416. @<Procedures and functions for handling numbers, characters, and strings@>=
  5417. procedure swap (@!swap1,@!swap2 : cite_number);
  5418. var innocent_bystander : cite_number;   {the temporary element in an exchange}
  5419. begin
  5420. innocent_bystander := sorted_cites[swap2];
  5421. sorted_cites[swap2] := sorted_cites[swap1];
  5422. sorted_cites[swap1] := innocent_bystander;
  5423. @:this can't happen}{\quad Duplicate sort key@>
  5424. The function |less_than| compares the two \.{sort.key\$}s indirectly
  5425. pointed to by its arguments and returns |true| if the first argument's
  5426. \.{sort.key\$} is lexicographically less than the second's (that is,
  5427. alphabetically earlier).  In case of ties the function compares the
  5428. indices |arg1| and |arg2|, which are assumed to be different, and
  5429. returns |true| if the first is smaller.  This function uses
  5430. |ASCII_code|s to compare, so it might give ``interesting'' results
  5431. when handling nonletters.
  5432. @d compare_return(#) == begin           {the compare is finished}
  5433.                         less_than := #;
  5434.                         return;
  5435.                         end
  5436. @<Procedures and functions for handling numbers, characters, and strings@>=
  5437. function less_than (@!arg1,@!arg2 : cite_number) : boolean;
  5438. label exit;
  5439. var char_ptr : 0..ent_str_size;         {character index into compared strings}
  5440.     @!ptr1,@!ptr2 : str_ent_loc;        {the two \.{sort.key\$} pointers}
  5441.     @!char1,@!char2 : ASCII_code;       {the two characters being compared}
  5442. begin
  5443. ptr1 := arg1*num_ent_strs + sort_key_num;
  5444. ptr2 := arg2*num_ent_strs + sort_key_num;
  5445. char_ptr := 0;
  5446.     begin
  5447.     char1 := entry_strs[ptr1][char_ptr];
  5448.     char2 := entry_strs[ptr2][char_ptr];
  5449.     if (char1 = end_of_string) then
  5450.         if (char2 = end_of_string) then
  5451.             if (arg1 < arg2) then
  5452.                 compare_return (true)
  5453.             else if (arg1 > arg2) then
  5454.                 compare_return (false)
  5455.             else                                {|arg1 = arg2|}
  5456.                 confusion ('Duplicate sort key')
  5457.         else                                    {|char2 <> end_of_string|}
  5458.             compare_return (true)
  5459.     else                                        {|char1 <> end_of_string|}
  5460.         if (char2 = end_of_string) then
  5461.             compare_return (false)
  5462.     else if (char1 < char2) then
  5463.         compare_return (true)
  5464.     else if (char1 > char2) then
  5465.         compare_return (false);
  5466.     incr(char_ptr);
  5467.     end;
  5468. exit:
  5469. The recursive procedure |quick_sort| sorts the entries indirectly
  5470. pointed to by the |sorted_cites| elements between |left_end| and
  5471. |right_end|, inclusive, based on the value of the |str_entry_var|
  5472. \.{sort.key\$}.  It's a fairly standard quicksort (for example, see
  5473. Algorithm 5.2.2Q in {\sl The Art of Computer Programming}), but uses
  5474. the median-of-three method to choose the partition element just in
  5475. case the entries are already sorted (or nearly sorted---humans and
  5476. ASCII might have different ideas on lexicographic ordering); it is a
  5477. stable sort.  This code generally prefers clarity to assembler-type
  5478. execution-time efficiency since |cite_list|s will rarely be huge.
  5479. The value |short_list|, which must be at least |2*end_offset + 2| for
  5480. this code to work, tells us the list-length at which the list is small
  5481. enough to warrant switching over to straight insertion sort from the
  5482. recursive quicksort.  The values here come from modest empirical tests
  5483. aimed at minimizing, for large |cite_list|s (five hundred or so), the
  5484. number of comparisons (between keys) plus the number of calls to
  5485. |quick_sort|.  The value |end_offset| must be positive; this helps
  5486. avoid $n^2$ behavior observed when the list starts out nearly, but not
  5487. completely, sorted (and fairly frequently large |cite_list|s come from
  5488. entire databases, which fairly frequently are nearly sorted).
  5489. @d short_list = 10      {use straight insertion sort at or below this length}
  5490. @d end_offset = 4       {the index end-offsets for choosing a median-of-three}
  5491. @<Check the ``constant'' values for consistency@>=
  5492. if (short_list < 2*end_offset + 2) then bad:=100*bad+22;
  5493. Here's the actual procedure.
  5494. @d next_insert = 24     {now insert the next element}
  5495. @<Procedures and functions for handling numbers, characters, and strings@>=
  5496. procedure quick_sort (@!left_end,@!right_end : cite_number);
  5497. label next_insert;
  5498. var left,@!right : cite_number;         {two general |sorted_cites| pointers}
  5499.     @!insert_ptr : cite_number;         {the to-be-(straight)-inserted element}
  5500.     @!middle : cite_number;     {the |(left_end+right_end) div 2| element}
  5501.     @!partition : cite_number;          {the median-of-three partition element}
  5502. begin
  5503.   trace
  5504.   trace_pr_ln ('Sorting ',left_end:0,' through ',right_end:0);
  5505.   ecart@/
  5506. if (right_end - left_end < short_list) then
  5507.     @<Do a straight insertion sort@>
  5508.   else
  5509.     begin
  5510.     @<Draw out the median-of-three partition element@>;
  5511.     @<Do the partitioning and the recursive calls@>;
  5512.     end;
  5513. This code sorts the entries between |left_end| and |right_end| when
  5514. the difference is less than |short_list|.  Each iteration of the outer
  5515. loop inserts the element indicated by |insert_ptr| into its proper
  5516. place among the (sorted) elements from |left_end| through
  5517. |insert_ptr-1|.
  5518. @<Do a straight insertion sort@>=
  5519. begin
  5520. for insert_ptr := left_end+1 to right_end do
  5521.     begin
  5522.     for right := insert_ptr downto left_end+1 do
  5523.         begin
  5524.         if (less_than (sorted_cites[right-1], sorted_cites[right])) then
  5525.             goto next_insert;
  5526.         swap (right-1, right);
  5527.         end;
  5528. next_insert:
  5529.     end;
  5530. Now we find the median of the three \.{sort.key\$}s to which the three
  5531. elements |sorted_cites[left_end+end_offset]|,
  5532. |sorted_cites[right_end]-end_offset|, and
  5533. |sorted_cites[(left_end+right_end) div 2]| point (a nonzero
  5534. |end_offset| avoids using as the leftmost of the three elements the
  5535. one that was swapped there when the old partition element was swapped
  5536. into its final spot; this turns out to avoid $n^2$ behavior when the
  5537. list is nearly sorted to start with).  This code determines which of
  5538. the six possible permutations we're dealing with and moves the median
  5539. element to |left_end|.  The comments next to the |swap| actions give
  5540. the known orderings of the corresponding elements of |sorted_cites|
  5541. before the action.
  5542. @<Draw out the median-of-three partition element@>=
  5543. begin
  5544. left := left_end + end_offset;
  5545. middle := (left_end+right_end) div 2;
  5546. right := right_end - end_offset;
  5547. if (less_than (sorted_cites[left], sorted_cites[middle])) then
  5548.   if (less_than (sorted_cites[middle], sorted_cites[right])) then
  5549.                                         {|left < middle < right|}
  5550.         swap(left_end,middle)
  5551.     else if (less_than (sorted_cites[left], sorted_cites[right])) then
  5552.                                         {|left < right < middle|}
  5553.         swap(left_end,right)
  5554.       else                              {|right < left < middle|}
  5555.         swap(left_end,left)
  5556.   else if (less_than (sorted_cites[right], sorted_cites[middle])) then
  5557.                                         {|right < middle < left|}
  5558.         swap(left_end,middle)
  5559.     else if (less_than (sorted_cites[right], sorted_cites[left])) then
  5560.                                         {|middle < right < left|}
  5561.         swap(left_end,right)
  5562.       else                              {|middle < left < right|}
  5563.         swap(left_end,left);
  5564. This module uses the median-of-three computed above to partition the
  5565. elements into those less than and those greater than the median.
  5566. Equal \.{sort.key\$}s are sorted by order of occurrence (in
  5567. |cite_list|).
  5568. @<Do the partitioning and the recursive calls@>=
  5569. begin
  5570. partition := sorted_cites[left_end];
  5571. left := left_end + 1;
  5572. right := right_end;
  5573. repeat
  5574.     while (less_than (sorted_cites[left], partition)) do
  5575.         incr(left);
  5576.     while (less_than (partition, sorted_cites[right])) do
  5577.         decr(right);
  5578.                 {now |sorted_cites[right] < partition < sorted_cites[left]|}
  5579.     if (left < right) then
  5580.         begin
  5581.         swap (left,right);
  5582.         incr(left);
  5583.         decr(right);
  5584.         end;
  5585. until (left = right+1); {pointers have crossed}
  5586. swap (left_end,right);{restoring the partition element to its |right|ful place}
  5587. quick_sort (left_end,right-1);
  5588. quick_sort (left,right_end);
  5589. @:BibTeX capacity exceeded}{\quad literal-stack size@>
  5590. @:this can't happen}{\quad Unknown literal type@>
  5591. Ok, that's it for sorting; now we'll play with the literal stack.
  5592. This procedure pushes a literal onto the stack, checking for stack
  5593. overflow.
  5594. @<Procedures and functions for style-file function execution@>=
  5595. procedure push_lit_stk (@!push_lt:integer; @!push_type:stk_type);
  5596.   trace
  5597.   var dum_ptr : lit_stk_loc;    {used just as an index variable}
  5598.   ecart@/
  5599. begin
  5600. lit_stack[lit_stk_ptr] := push_lt;
  5601. lit_stk_type[lit_stk_ptr] := push_type;
  5602.   trace
  5603.   for dum_ptr := 0 to lit_stk_ptr do
  5604.     trace_pr ('  ');
  5605.   trace_pr ('Pushing ');
  5606.   case (lit_stk_type[lit_stk_ptr]) of
  5607.     stk_int : trace_pr_ln (lit_stack[lit_stk_ptr]:0);
  5608.     stk_str : begin
  5609.               trace_pr ('"');
  5610.               trace_pr_pool_str (lit_stack[lit_stk_ptr]);
  5611.               trace_pr_ln ('"');
  5612.               end;
  5613.     stk_fn : begin
  5614.              trace_pr ('`');
  5615.              trace_pr_pool_str (hash_text[lit_stack[lit_stk_ptr]]);
  5616.              trace_pr_ln ('''');
  5617.              end;
  5618.     stk_field_missing : begin
  5619.                         trace_pr ('missing field `');
  5620.                         trace_pr_pool_str (lit_stack[lit_stk_ptr]);
  5621.                         trace_pr_ln ('''');
  5622.                         end;
  5623.     stk_empty : trace_pr_ln ('a bad literal--popped from an empty stack');
  5624.     othercases unknwn_literal_confusion
  5625.   endcases;
  5626.   ecart@/
  5627. if (lit_stk_ptr = lit_stk_size) then
  5628.     overflow('literal-stack size ',lit_stk_size);
  5629. incr(lit_stk_ptr);
  5630. @^push the literal stack@>
  5631. This macro pushes the last thing, necessarily a string, that was
  5632. popped.  And this module, along with others that push the literal
  5633. stack without explicitly calling |push_lit_stack|, have an index entry
  5634. under ``push the literal stack''; these implicit pushes collectively
  5635. speed up the program by about ten percent.
  5636. @d repush_string == begin
  5637.                     if (lit_stack[lit_stk_ptr] >= cmd_str_ptr) then
  5638.                         unflush_string;
  5639.                     incr(lit_stk_ptr);
  5640.                     end
  5641. @:this can't happen}{\quad Nontop top of string stack@>
  5642. This procedure pops the stack, checking for, and trying to recover
  5643. from, stack underflow.  (Actually, this procedure is really a
  5644. function, since it returns the two values through its |var|
  5645. parameters.)  Also, if the literal being popped is a |stk_str| that's
  5646. been created during the execution of the current \.{.bst} command, pop
  5647. it from |str_pool| as well (it will be the string corresponding to
  5648. |str_ptr-1|).  Note that when this happens, the string is no longer
  5649. `officially' available so that it must be used before anything else is
  5650. added to |str_pool|.
  5651. @<Procedures and functions for style-file function execution@>=
  5652. procedure pop_lit_stk (var pop_lit:integer; var pop_type:stk_type);
  5653. begin
  5654. if (lit_stk_ptr = 0) then
  5655.     begin
  5656.     bst_ex_warn ('You can''t pop an empty literal stack');@/
  5657.     pop_type := stk_empty;      {this is an error recovery attempt}
  5658.     end
  5659.   else
  5660.     begin
  5661.     decr(lit_stk_ptr);
  5662.     pop_lit := lit_stack[lit_stk_ptr];
  5663.     pop_type := lit_stk_type[lit_stk_ptr];
  5664.     if (pop_type = stk_str) then
  5665.       if (pop_lit >= cmd_str_ptr) then
  5666.         begin
  5667.         if (pop_lit <> str_ptr-1) then
  5668.             confusion ('Nontop top of string stack');
  5669.         flush_string;
  5670.         end;
  5671.     end;
  5672. @:this can't happen}{\quad Illegal literal type@>
  5673. @:this can't happen}{\quad Unknown literal type@>
  5674. More bug complaints, this time about bad literals.
  5675. @<Procedures and functions for all file I/O, error messages, and such@>=
  5676. procedure illegl_literal_confusion;
  5677. begin
  5678. confusion ('Illegal literal type');
  5679. procedure unknwn_literal_confusion;
  5680. begin
  5681. confusion ('Unknown literal type');
  5682. @:this can't happen}{\quad Illegal literal type@>
  5683. @:this can't happen}{\quad Unknown literal type@>
  5684. Occasionally we'll want to know what's on the literal stack.  Here we
  5685. print out a stack literal, giving its type.  This procedure should
  5686. never be called after popping an empty stack.
  5687. @<Procedures and functions for all file I/O, error messages, and such@>=
  5688. procedure print_stk_lit (@!stk_lt:integer; @!stk_tp:stk_type);
  5689. begin
  5690. case (stk_tp) of
  5691.     stk_int : print (stk_lt:0,' is an integer literal');
  5692.     stk_str : begin
  5693.               print ('"');
  5694.               print_pool_str (stk_lt);
  5695.               print ('" is a string literal');
  5696.               end;
  5697.     stk_fn : begin
  5698.              print ('`');
  5699.              print_pool_str (hash_text[stk_lt]);
  5700.              print (''' is a function literal');
  5701.              end;
  5702.     stk_field_missing : begin
  5703.                         print ('`');
  5704.                         print_pool_str (stk_lt);
  5705.                         print (''' is a missing field');
  5706.                         end;
  5707.     stk_empty : illegl_literal_confusion;
  5708.     othercases unknwn_literal_confusion
  5709. endcases;
  5710. @:this can't happen}{\quad Illegal literal type@>
  5711. @:this can't happen}{\quad Unknown literal type@>
  5712. This procedure appropriately chastises the style designer; however, if
  5713. the wrong literal came from popping an empty stack, the procedure
  5714. |pop_lit_stack| will have already done the chastising (because this
  5715. procedure is called only after popping the stack) so there's no need
  5716. for more.
  5717. @<Procedures and functions for style-file function execution@>=
  5718. procedure print_wrong_stk_lit (@!stk_lt:integer; @!stk_tp1,@!stk_tp2:stk_type);
  5719. begin
  5720. if (stk_tp1 <> stk_empty) then
  5721.     begin
  5722.     print_stk_lit (stk_lt, stk_tp1);
  5723.     case (stk_tp2) of
  5724.         stk_int : print (', not an integer,');
  5725.         stk_str : print (', not a string,');
  5726.         stk_fn : print (', not a function,');
  5727.         stk_field_missing,
  5728.         stk_empty : illegl_literal_confusion;
  5729.         othercases unknwn_literal_confusion
  5730.     endcases;
  5731.     bst_ex_warn_print;
  5732.     end;
  5733. @:this can't happen}{\quad Illegal literal type@>
  5734. @:this can't happen}{\quad Unknown literal type@>
  5735. This is similar to |print_stk_lit|, but here we don't give the
  5736. literal's type, and here we end with a new line.  This procedure
  5737. should never be called after popping an empty stack.
  5738. @<Procedures and functions for all file I/O, error messages, and such@>=
  5739. procedure print_lit (@!stk_lt:integer; @!stk_tp:stk_type);
  5740. begin
  5741. case (stk_tp) of
  5742.     stk_int : print_ln (stk_lt:0);
  5743.     stk_str : begin
  5744.               print_pool_str (stk_lt);
  5745.               print_newline;
  5746.               end;
  5747.     stk_fn : begin
  5748.              print_pool_str (hash_text[stk_lt]);
  5749.              print_newline;
  5750.              end;
  5751.     stk_field_missing : begin
  5752.                         print_pool_str (stk_lt);
  5753.                         print_newline;
  5754.                         end;
  5755.     stk_empty : illegl_literal_confusion;
  5756.     othercases unknwn_literal_confusion
  5757. endcases;
  5758. This procedure pops and prints the top of the stack; when the stack is
  5759. empty the procedure |pop_lit_stk| complains.
  5760. @<Procedures and functions for style-file function execution@>=
  5761. procedure pop_top_and_print;
  5762. var stk_lt : integer;
  5763.   @!stk_tp : stk_type;
  5764. begin
  5765. pop_lit_stk (stk_lt,stk_tp);
  5766. if (stk_tp = stk_empty) then
  5767.     print_ln ('Empty literal')
  5768.   else
  5769.     print_lit (stk_lt,stk_tp);
  5770. This procedure pops and prints the whole stack.
  5771. @<Procedures and functions for style-file function execution@>=
  5772. procedure pop_whole_stack;
  5773. begin
  5774. while (lit_stk_ptr > 0) do
  5775.     pop_top_and_print;
  5776. At the beginning of a \.{.bst}-command execution we make the stack
  5777. empty and record how much of |str_pool| has been used.
  5778. @<Procedures and functions for style-file function execution@>=
  5779. procedure init_command_execution;
  5780. begin
  5781. lit_stk_ptr := 0;       {make the stack empty}
  5782. cmd_str_ptr := str_ptr; {we'll check this when we finish command execution}
  5783. @:this can't happen}{\quad Nonempty empty string stack@>
  5784. At the end of a \.{.bst} command-execution we check that the stack and
  5785. |str_pool| are still in good shape.
  5786. @<Procedures and functions for style-file function execution@>=
  5787. procedure check_command_execution;
  5788. begin
  5789. if (lit_stk_ptr<>0) then
  5790.     begin
  5791.     print_ln ('ptr=',lit_stk_ptr:0,', stack=');
  5792.     pop_whole_stack;
  5793.     bst_ex_warn ('---the literal stack isn''t empty');
  5794.     end;
  5795. if (cmd_str_ptr<>str_ptr) then
  5796.     begin
  5797.       trace
  5798.       print_ln ('Pointer is ',str_ptr:0,' but should be ',cmd_str_ptr:0);
  5799.       ecart@/
  5800.     confusion ('Nonempty empty string stack');
  5801.     end;
  5802. This procedure adds to |str_pool| the string from |ex_buf[0]| through
  5803. |ex_buf[ex_buf_length-1]| if it will fit.  It assumes the global
  5804. variable |ex_buf_length| gives the length of the current string in
  5805. |ex_buf|.  It then pushes this string onto the literal stack.
  5806. @<Procedures and functions for style-file function execution@>=
  5807. procedure add_pool_buf_and_push;
  5808. begin
  5809. str_room (ex_buf_length);               {make sure this string will fit}
  5810. ex_buf_ptr := 0;
  5811. while (ex_buf_ptr < ex_buf_length) do
  5812.     begin
  5813.     append_char (ex_buf[ex_buf_ptr]);
  5814.     incr(ex_buf_ptr);
  5815.     end;
  5816. push_lit_stk (make_string, stk_str);    {and push it onto the stack}
  5817. @:BibTeX capacity exceeded}{\quad buffer size@>
  5818. These macros append a character to |ex_buf|.  Which is called depends
  5819. on whether the character is known to fit.
  5820. @d append_ex_buf_char(#) == begin
  5821.                             ex_buf[ex_buf_ptr] := #;
  5822.                             incr(ex_buf_ptr);
  5823.                             end
  5824. @d append_ex_buf_char_and_check(#) ==
  5825.                             begin
  5826.                             if (ex_buf_ptr = buf_size) then
  5827.                                 buffer_overflow;
  5828.                             append_ex_buf_char(#);
  5829.                             end
  5830. @:BibTeX capacity exceeded}{\quad buffer size@>
  5831. This procedure adds to the execution buffer the given string in
  5832. |str_pool| if it will fit.  It assumes the global variable
  5833. |ex_buf_length| gives the length of the current string in |ex_buf|,
  5834. and thus also gives the location of the next character.
  5835. @<Procedures and functions for style-file function execution@>=
  5836. procedure add_buf_pool (@!p_str : str_number);
  5837. begin
  5838. p_ptr1 := str_start[p_str];
  5839. p_ptr2 := str_start[p_str+1];
  5840. if (ex_buf_length+(p_ptr2-p_ptr1) > buf_size) then
  5841.     buffer_overflow;
  5842. ex_buf_ptr := ex_buf_length;
  5843. while (p_ptr1 < p_ptr2) do
  5844.     begin                       {copy characters into the buffer}
  5845.     append_ex_buf_char (str_pool[p_ptr1]);
  5846.     incr(p_ptr1);
  5847.     end;
  5848. ex_buf_length := ex_buf_ptr;
  5849. This procedure actually writes onto the \.{.bbl}~file a line of output
  5850. (the characters from |out_buf[0]| to |out_buf[out_buf_length-1]|,
  5851. after removing trailing |white_space| characters).  It also updates
  5852. |bbl_line_num|, the line counter.  It writes a blank line if and only
  5853. if |out_buf| is empty.  The program uses this procedure in such a way
  5854. that |out_buf| will be nonempty if there have been characters put in
  5855. it since the most recent \.{newline\$}.
  5856. @<Procedures and functions for all file I/O, error messages, and such@>=
  5857. procedure output_bbl_line;
  5858. label loop_exit,@!exit;
  5859. begin
  5860. if (out_buf_length <> 0) then           {the buffer's not empty}
  5861.     begin
  5862.     while (out_buf_length > 0) do       {remove trailing |white_space|}
  5863.       if (lex_class[out_buf[out_buf_length-1]] = white_space) then
  5864.         decr(out_buf_length)
  5865.        else
  5866.         goto loop_exit;
  5867. loop_exit:
  5868.     if (out_buf_length = 0) then        {ignore a line of just |white_space|}
  5869.         return;
  5870.     out_buf_ptr := 0;
  5871.     while (out_buf_ptr < out_buf_length) do
  5872.         begin
  5873.         write (bbl_file, xchr[out_buf[out_buf_ptr]]);
  5874.         incr(out_buf_ptr);
  5875.         end;
  5876.     end;
  5877. write_ln (bbl_file);
  5878. incr(bbl_line_num);     {update line number}
  5879. out_buf_length := 0;    {make the next line empty}
  5880. exit:
  5881. @:BibTeX capacity exceeded}{\quad output buffer size@>
  5882. This procedure adds to the output buffer the given string in
  5883. |str_pool|.  It assumes the global variable |out_buf_length| gives the
  5884. length of the current string in |out_buf|, and thus also gives the
  5885. location for the next character.  If there are enough characters
  5886. present in the output buffer, it writes one or more lines out to the
  5887. \.{.bbl} file.  It may break a line at any |white_space| character it
  5888. likes, but if it does, it will add two |space|s to the next output
  5889. line.
  5890. @<Procedures and functions for style-file function execution@>=
  5891. procedure add_out_pool (@!p_str : str_number);
  5892. var break_ptr : buf_pointer;    {the first character following the line break}
  5893. @!end_ptr : buf_pointer;        {temporary end-of-buffer pointer}
  5894. begin
  5895. p_ptr1 := str_start[p_str];
  5896. p_ptr2 := str_start[p_str+1];
  5897. if (out_buf_length+(p_ptr2-p_ptr1) > buf_size) then
  5898.     overflow('output buffer size ',buf_size);
  5899. out_buf_ptr := out_buf_length;
  5900. while (p_ptr1 < p_ptr2) do
  5901.     begin                       {copy characters into the buffer}
  5902.     out_buf[out_buf_ptr] := str_pool[p_ptr1];
  5903.     incr(p_ptr1);
  5904.     incr(out_buf_ptr);
  5905.     end;
  5906. out_buf_length := out_buf_ptr;
  5907. while (out_buf_length > max_print_line) do
  5908.     @<Break that line@>;
  5909. Here we break the line by looking for a |white_space| character,
  5910. backwards from |out_buf[max_print_line]| until
  5911. |out_buf[min_print_line]|; we break at the |white_space| and indent
  5912. the next line two |space|s.  The next module handles things when
  5913. there's no |white_space| character to break at.
  5914. @<Break that line@>=
  5915. begin
  5916. end_ptr := out_buf_length;
  5917. out_buf_ptr := max_print_line;
  5918. while ((lex_class[out_buf[out_buf_ptr]] <> white_space) and
  5919.                                         (out_buf_ptr >= min_print_line)) do
  5920.     decr(out_buf_ptr);
  5921. if (out_buf_ptr = min_print_line-1) then        {no |white_space| character}
  5922.     @<Break that unbreakable line@>
  5923.     begin                                       {hit a |white_space| character}
  5924.     out_buf_length := out_buf_ptr;
  5925.     break_ptr := out_buf_length + 1;
  5926.     output_bbl_line;                    {output what we can}
  5927.     out_buf[0] := space;
  5928.     out_buf[1] := space;                {start the next line with two |space|s}
  5929.     out_buf_ptr := 2;
  5930.     tmp_ptr := break_ptr;
  5931.     while (tmp_ptr < end_ptr) do        {and slide the rest down}
  5932.         begin
  5933.         out_buf[out_buf_ptr] := out_buf[tmp_ptr];
  5934.         incr(out_buf_ptr);
  5935.         incr(tmp_ptr);
  5936.         end;
  5937.     out_buf_length := end_ptr - break_ptr + 2;
  5938.     end;
  5939. If there's no |white_space| character to break the line at, we break
  5940. it at |out_buf[max_print_line-1]|, append a |comment| character, and
  5941. don't indent the next line.
  5942. @<Break that unbreakable line@>=
  5943. begin
  5944. out_buf[end_ptr] := out_buf[max_print_line-1];  {save this character}
  5945. out_buf[max_print_line-1] := comment;           {so \TeX\ does the thing right}
  5946. out_buf_length := max_print_line;
  5947. break_ptr := out_buf_length - 1;        {the `|-1|' allows for the restoration}
  5948. output_bbl_line;                                {output what we can,}
  5949. out_buf[max_print_line-1] := out_buf[end_ptr];  {restore this character}
  5950. out_buf_ptr := 0;
  5951. tmp_ptr := break_ptr;
  5952. while (tmp_ptr < end_ptr) do                    {and slide the rest down}
  5953.     begin
  5954.     out_buf[out_buf_ptr] := out_buf[tmp_ptr];
  5955.     incr(out_buf_ptr);
  5956.     incr(tmp_ptr);
  5957.     end;
  5958. out_buf_length := end_ptr - break_ptr;
  5959. @^Tuesdays@>
  5960. @^windows@>
  5961. @:this can't happen}{\quad Unknown function class@>
  5962. This procedure executes a single specified function; it is the single
  5963. execution-primitive that does everything (except windows, and it takes
  5964. Tuesdays off).
  5965. @<|execute_fn| itself@>=
  5966. procedure execute_fn (@!ex_fn_loc : hash_loc);
  5967. @<Declarations for executing |built_in| functions@>
  5968. @!wiz_ptr : wiz_fn_loc;         {general |wiz_functions| location}
  5969. begin
  5970.   trace
  5971.   trace_pr ('execute_fn `');
  5972.   trace_pr_pool_str (hash_text[ex_fn_loc]);
  5973.   trace_pr_ln ('''');
  5974.   ecart@/
  5975. case (fn_type[ex_fn_loc]) of
  5976.     built_in : @<Execute a |built_in| function@>;
  5977.     wiz_defined : @<Execute a |wiz_defined| function@>;
  5978.     int_literal : push_lit_stk (fn_info[ex_fn_loc], stk_int);
  5979.     str_literal : push_lit_stk (hash_text[ex_fn_loc], stk_str);
  5980.     field : @<Execute a field@>;
  5981.     int_entry_var : @<Execute an |int_entry_var|@>;
  5982.     str_entry_var : @<Execute a |str_entry_var|@>;
  5983.     int_global_var : push_lit_stk (fn_info[ex_fn_loc], stk_int);
  5984.     str_global_var : @<Execute a |str_global_var|@>;
  5985.     othercases unknwn_function_class_confusion
  5986. endcases;
  5987. To execute a |wiz_defined| function, we just execute all those
  5988. functions in its definition, except that the special marker
  5989. |quote_next_fn| means we push the next function onto the stack.
  5990. @<Execute a |wiz_defined| function@>=
  5991. begin
  5992. wiz_ptr := fn_info[ex_fn_loc];
  5993. while (wiz_functions[wiz_ptr] <> end_of_def) do
  5994.     begin
  5995.     if (wiz_functions[wiz_ptr] <> quote_next_fn) then
  5996.         execute_fn (wiz_functions[wiz_ptr])
  5997.       else
  5998.         begin
  5999.         incr(wiz_ptr);
  6000.         push_lit_stk (wiz_functions[wiz_ptr], stk_fn);
  6001.         end;
  6002.     incr(wiz_ptr);
  6003.     end;
  6004. This module pushes the string given by the field onto the literal
  6005. stack unless it's |missing|, in which case it pushes a special value
  6006. onto the stack.
  6007. @<Execute a field@>=
  6008. begin
  6009. if (not mess_with_entries) then
  6010.     bst_cant_mess_with_entries_print
  6011.   else
  6012.     begin
  6013.     field_ptr := cite_ptr*num_fields + fn_info[ex_fn_loc];
  6014.     if (field_info[field_ptr] = missing) then
  6015.         push_lit_stk (hash_text[ex_fn_loc], stk_field_missing)
  6016.       else
  6017.         push_lit_stk (field_info[field_ptr], stk_str);
  6018.     end
  6019. This module pushes the integer given by an |int_entry_var| onto the
  6020. literal stack.
  6021. @<Execute an |int_entry_var|@>=
  6022. begin
  6023. if (not mess_with_entries) then
  6024.     bst_cant_mess_with_entries_print
  6025.   else
  6026.     push_lit_stk (entry_ints[cite_ptr*num_ent_ints+fn_info[ex_fn_loc]],
  6027.                                                                 stk_int);
  6028. This module adds the string given by a |str_entry_var| to |str_pool|
  6029. via the execution buffer and pushes it onto the literal stack.
  6030. @<Execute a |str_entry_var|@>=
  6031. begin
  6032. if (not mess_with_entries) then
  6033.     bst_cant_mess_with_entries_print
  6034.   else
  6035.     begin
  6036.     str_ent_ptr := cite_ptr*num_ent_strs + fn_info[ex_fn_loc];@/
  6037.     ex_buf_ptr := 0;                    {also serves as |ent_chr_ptr|}
  6038.     while (entry_strs[str_ent_ptr][ex_buf_ptr] <> end_of_string) do
  6039.                                         {copy characters into the buffer}
  6040.         append_ex_buf_char (entry_strs[str_ent_ptr][ex_buf_ptr]);
  6041.     ex_buf_length := ex_buf_ptr;
  6042.     add_pool_buf_and_push;              {push this string onto the stack}
  6043.     end;
  6044. This module pushes the string given by a |str_global_var| onto the
  6045. literal stack, but it copies the string to |str_pool| (character by
  6046. character) only if it has to---it {\it doesn't\/} have to if the
  6047. string is static (that is, if the string isn't at the top, temporary
  6048. part of the string pool).
  6049. @<Execute a |str_global_var|@>=
  6050. begin
  6051. str_glb_ptr := fn_info[ex_fn_loc];
  6052. if (glb_str_ptr[str_glb_ptr] > 0) then  {we're dealing with a static string}
  6053.     push_lit_stk (glb_str_ptr[str_glb_ptr],stk_str)
  6054.   else
  6055.     begin
  6056.     str_room(glb_str_end[str_glb_ptr]);
  6057.     glob_chr_ptr := 0;
  6058.     while (glob_chr_ptr < glb_str_end[str_glb_ptr]) do  {copy the string}
  6059.         begin
  6060.         append_char (global_strs[str_glb_ptr][glob_chr_ptr]);
  6061.         incr(glob_chr_ptr);
  6062.         end;
  6063.     push_lit_stk (make_string, stk_str);        {and push it onto the stack}
  6064.     end;
  6065. @* The built-in functions.
  6066. @^add a built-in function@>
  6067. @^biblical procreation@>
  6068. @^grade inflation@>
  6069. This section gives the all the code for all the built-in functions
  6070. (including pre-defined |field|s, |str_entry_var|s, and
  6071. |int_global_var|s, which technically aren't classified as |built_in|).
  6072. To modify or add one, we needn't go anywhere else (with one exception:
  6073. The constant |max_pop|, which gives the maximum number of literals
  6074. that any of these functions pops off the stack, is defined earlier
  6075. because it's needed earlier; thus, if we need to update it, which will
  6076. happen if some new |built_in| functions uses more than |max_pop|
  6077. literals from the stack, we'll have to go outside this section).
  6078. Adding a |built_in| function entails modifying (at least four of) the
  6079. five modules marked by ``add a built-in function'' in the index, in
  6080. addition to adding the code to execute the function.
  6081. These variables all begin with |b_| and specify the hash-table
  6082. locations of the |built_in| functions, except that |b_default| is
  6083. pseudo-|built_in|---either it will point to the no-op \.{skip\$} or to
  6084. the \.{.bst}-defined function \.{default.type}; it's used when an
  6085. entry has a type that's not defined in the \.{.bst} file.
  6086. @<Globals in the outer block@>=
  6087. @!b_equals : hash_loc;          {\.{=}}
  6088. @!b_greater_than : hash_loc;    {\.{>}}
  6089. @!b_less_than : hash_loc;       {\.{<}}
  6090. @!b_plus : hash_loc;            {\.{+} (this may be changed to an |a_minus|)}
  6091. @!b_minus : hash_loc;           {\.{-}}
  6092. @!b_concatenate : hash_loc;     {\.{*}}
  6093. @!b_gets : hash_loc;            {\.{:=} (formerly, |b_gat|)}
  6094. @!b_add_period : hash_loc;      {\.{add.period\$}}
  6095. @!b_call_type : hash_loc;       {\.{call.type\$}}
  6096. @!b_change_case : hash_loc;     {\.{change.case\$}}
  6097. @!b_chr_to_int : hash_loc;      {\.{chr.to.int\$}}
  6098. @!b_cite : hash_loc;            {\.{cite\$}}
  6099. @!b_duplicate : hash_loc;       {\.{duplicate\$}}
  6100. @!b_empty : hash_loc;           {\.{empty\$}}
  6101. @!b_format_name : hash_loc;     {\.{format.name\$}}
  6102. @!b_if : hash_loc;              {\.{if\$}}
  6103. @!b_int_to_chr : hash_loc;      {\.{int.to.chr\$}}
  6104. @!b_int_to_str : hash_loc;      {\.{int.to.str\$}}
  6105. @!b_missing : hash_loc;         {\.{missing\$}}
  6106. @!b_newline : hash_loc;         {\.{newline\$}}
  6107. @!b_num_names : hash_loc;       {\.{num.names\$}}
  6108. @!b_pop : hash_loc;             {\.{pop\$}}
  6109. @!b_preamble : hash_loc;        {\.{preamble\$}}
  6110. @!b_purify : hash_loc;          {\.{purify\$}}
  6111. @!b_quote : hash_loc;           {\.{quote\$}}
  6112. @!b_skip : hash_loc;            {\.{skip\$}}
  6113. @!b_stack : hash_loc;           {\.{stack\$}}
  6114. @!b_substring : hash_loc;       {\.{substring\$}}
  6115. @!b_swap : hash_loc;            {\.{swap\$}}
  6116. @!b_text_length : hash_loc;     {\.{text.length\$}}
  6117. @!b_text_prefix : hash_loc;     {\.{text.prefix\$}}
  6118. @!b_top_stack : hash_loc;       {\.{top\$}}
  6119. @!b_type : hash_loc;            {\.{type\$}}
  6120. @!b_warning : hash_loc;         {\.{warning\$}}
  6121. @!b_while : hash_loc;           {\.{while\$}}
  6122. @!b_width : hash_loc;           {\.{width\$}}
  6123. @!b_write : hash_loc;           {\.{write\$}}
  6124. @!b_default : hash_loc;         {either \.{skip\$} or \.{default.type}}
  6125.   stat
  6126.   @!blt_in_loc : array[blt_in_range] of hash_loc; {for execution counts}
  6127.   @!execution_count : array[blt_in_range] of integer; {the same}
  6128.   @!total_ex_count : integer;           {the sum of all |execution_count|s}
  6129.   @!blt_in_ptr : blt_in_range;          {a pointer into |blt_in_loc|}
  6130.   tats@/
  6131. Where |blt_in_range| gives the legal |built_in| function numbers.
  6132. @<Types in the outer block@>=
  6133. @!blt_in_range = 0..num_blt_in_fns;
  6134. @^add a built-in function@>
  6135. These constants all begin with |n_| and are used for the |case|
  6136. statement that determines which |built_in| function to execute.
  6137. @d n_equals = 0         {\.{=}}
  6138. @d n_greater_than = 1   {\.{>}}
  6139. @d n_less_than = 2      {\.{<}}
  6140. @d n_plus = 3           {\.{+}}
  6141. @d n_minus = 4          {\.{-}}
  6142. @d n_concatenate = 5    {\.{*}}
  6143. @d n_gets = 6           {\.{:=}}
  6144. @d n_add_period = 7     {\.{add.period\$}}
  6145. @d n_call_type = 8      {\.{call.type\$}}
  6146. @d n_change_case = 9    {\.{change.case\$}}
  6147. @d n_chr_to_int = 10    {\.{chr.to.int\$}}
  6148. @d n_cite = 11          {\.{cite\$} (this may start a riot)}
  6149. @d n_duplicate = 12     {\.{duplicate\$}}
  6150. @d n_empty = 13         {\.{empty\$}}
  6151. @d n_format_name = 14   {\.{format.name\$}}
  6152. @d n_if = 15            {\.{if\$}}
  6153. @d n_int_to_chr = 16    {\.{int.to.chr\$}}
  6154. @d n_int_to_str = 17    {\.{int.to.str\$}}
  6155. @d n_missing = 18       {\.{missing\$}}
  6156. @d n_newline = 19       {\.{newline\$}}
  6157. @d n_num_names = 20     {\.{num.names\$}}
  6158. @d n_pop = 21           {\.{pop\$}}
  6159. @d n_preamble = 22      {\.{preamble\$}}
  6160. @d n_purify = 23        {\.{purify\$}}
  6161. @d n_quote = 24         {\.{quote\$}}
  6162. @d n_skip = 25          {\.{skip\$}}
  6163. @d n_stack = 26         {\.{stack\$}}
  6164. @d n_substring = 27     {\.{substring\$}}
  6165. @d n_swap = 28          {\.{swap\$}}
  6166. @d n_text_length = 29   {\.{text.length\$}}
  6167. @d n_text_prefix = 30   {\.{text.prefix\$}}
  6168. @d n_top_stack = 31     {\.{top\$}}
  6169. @d n_type = 32          {\.{type\$}}
  6170. @d n_warning = 33       {\.{warning\$}}
  6171. @d n_while = 34         {\.{while\$}}
  6172. @d n_width = 35         {\.{width\$}}
  6173. @d n_write = 36         {\.{write\$}}
  6174. @<Constants in the outer block@>=
  6175. @!num_blt_in_fns = 37;  {one more than the previous number}
  6176. @^add a built-in function@>
  6177. @^important note@>
  6178. It's time for us to insert more pre-defined strings into |str_pool|
  6179. (and thus the hash table) and to insert the |built_in| functions into
  6180. the hash table.  The strings corresponding to these functions should
  6181. contain no upper-case letters, and they must all be exactly
  6182. |longest_pds| characters long.  The |build_in| routine (to appear
  6183. shortly) does the work.
  6184. Important note: These pre-definitions must not have any glitches or the
  6185. program may bomb because the |log_file| hasn't been opened yet.
  6186. @<Pre-define certain strings@>=
  6187. build_in('=           ',1,b_equals,n_equals);
  6188. build_in('>           ',1,b_greater_than,n_greater_than);
  6189. build_in('<           ',1,b_less_than,n_less_than);
  6190. build_in('+           ',1,b_plus,n_plus);
  6191. build_in('-           ',1,b_minus,n_minus);
  6192. build_in('*           ',1,b_concatenate,n_concatenate);
  6193. build_in(':=          ',2,b_gets,n_gets);
  6194. build_in('add.period$ ',11,b_add_period,n_add_period);
  6195. build_in('call.type$  ',10,b_call_type,n_call_type);
  6196. build_in('change.case$',12,b_change_case,n_change_case);
  6197. build_in('chr.to.int$ ',11,b_chr_to_int,n_chr_to_int);
  6198. build_in('cite$       ',5,b_cite,n_cite);
  6199. build_in('duplicate$  ',10,b_duplicate,n_duplicate);
  6200. build_in('empty$      ',6,b_empty,n_empty);
  6201. build_in('format.name$',12,b_format_name,n_format_name);
  6202. build_in('if$         ',3,b_if,n_if);
  6203. build_in('int.to.chr$ ',11,b_int_to_chr,n_int_to_chr);
  6204. build_in('int.to.str$ ',11,b_int_to_str,n_int_to_str);
  6205. build_in('missing$    ',8,b_missing,n_missing);
  6206. build_in('newline$    ',8,b_newline,n_newline);
  6207. build_in('num.names$  ',10,b_num_names,n_num_names);
  6208. build_in('pop$        ',4,b_pop,n_pop);
  6209. build_in('preamble$   ',9,b_preamble,n_preamble);
  6210. build_in('purify$     ',7,b_purify,n_purify);
  6211. build_in('quote$      ',6,b_quote,n_quote);
  6212. build_in('skip$       ',5,b_skip,n_skip);
  6213. build_in('stack$      ',6,b_stack,n_stack);
  6214. build_in('substring$  ',10,b_substring,n_substring);
  6215. build_in('swap$       ',5,b_swap,n_swap);
  6216. build_in('text.length$',12,b_text_length,n_text_length);
  6217. build_in('text.prefix$',12,b_text_prefix,n_text_prefix);
  6218. build_in('top$        ',4,b_top_stack,n_top_stack);
  6219. build_in('type$       ',5,b_type,n_type);
  6220. build_in('warning$    ',8,b_warning,n_warning);
  6221. build_in('width$      ',6,b_width,n_width);
  6222. build_in('while$      ',6,b_while,n_while);
  6223. build_in('width$      ',6,b_width,n_width);
  6224. build_in('write$      ',6,b_write,n_write);
  6225. This procedure inserts a |built_in| function into the hash table and
  6226. initializes the corresponding pre-defined string (of length at most
  6227. |longest_pds|).  The array |fn_info| contains a number from 0 through
  6228. the number of |built_in| functions minus 1 (i.e., |num_blt_in_fns - 1|
  6229. if we're keeping statistics); this number is used by a |case|
  6230. statement to execute this function and is used for keeping execution
  6231. counts when keeping statistics.
  6232. @<Procedures and functions for handling numbers, characters, and strings@>=
  6233. procedure build_in (@!pds:pds_type; @!len:pds_len; var fn_hash_loc:hash_loc;
  6234.                                         @!blt_in_num:blt_in_range);
  6235. begin
  6236. pre_define (pds,len,bst_fn_ilk);@/
  6237. fn_hash_loc := pre_def_loc;     {the |pre_define| routine sets |pre_def_loc|}
  6238. fn_type[fn_hash_loc] := built_in;
  6239. fn_info[fn_hash_loc] := blt_in_num;
  6240.   stat
  6241.   blt_in_loc[blt_in_num] := fn_hash_loc;@/
  6242.   execution_count[blt_in_num] := 0; {initialize the function-execution count}
  6243.   tats@/
  6244. This is a procedure so that |initialize| is smaller.
  6245. @<Procedures and functions for handling numbers, characters, and strings@>=
  6246. procedure pre_def_certain_strings;
  6247. begin
  6248. @<Pre-define certain strings@>@;
  6249. These variables all begin with |s_| and specify the locations in
  6250. |str_pool| of certain often-used strings that the \.{.bst} commands
  6251. need.  The |s_preamble| array is big enough to allow an average of one
  6252. \.{preamble\$} command per \.{.bib} file.
  6253. @<Globals in the outer block@>=
  6254. @!s_null : str_number;          {the null string}
  6255. @!s_default : str_number;       {\.{default.type}, for unknown entry types}
  6256. @!s_t : str_number;             {\.{t}, for |title_lowers| case conversion}
  6257. @!s_l : str_number;             {\.{l}, for |all_lowers| case conversion}
  6258. @!s_u : str_number;             {\.{u}, for |all_uppers| case conversion}
  6259. @!s_preamble : array[bib_number] of str_number;
  6260.                                 {for the \.{preamble\$} |built_in| function}
  6261. These constants all begin with |n_| and are used for the |case|
  6262. statement that determines which, if any, control sequence we're
  6263. dealing with; a control sequence of interest will be either one of the
  6264. undotted characters `\.{\\i}' or `\.{\\j}' or one of the foreign
  6265. characters in Table~3.2 of the \LaTeX\ manual.
  6266. @d n_i = 0              {\.{i}, for the undotted character \.{\\i}}
  6267. @d n_j = 1              {\.{j}, for the undotted character \.{\\j}}
  6268. @d n_oe = 2             {\.{oe}, for the foreign character \.{\\oe}}
  6269. @d n_oe_upper = 3       {\.{OE}, for the foreign character \.{\\OE}}
  6270. @d n_ae = 4             {\.{ae}, for the foreign character \.{\\ae}}
  6271. @d n_ae_upper = 5       {\.{AE}, for the foreign character \.{\\AE}}
  6272. @d n_aa = 6             {\.{aa}, for the foreign character \.{\\aa}}
  6273. @d n_aa_upper = 7       {\.{AA}, for the foreign character \.{\\AA}}
  6274. @d n_o = 8              {\.{o}, for the foreign character \.{\\o}}
  6275. @d n_o_upper = 9        {\.{O}, for the foreign character \.{\\O}}
  6276. @d n_l = 10             {\.{l}, for the foreign character \.{\\l}}
  6277. @d n_l_upper = 11       {\.{L}, for the foreign character \.{\\L}}
  6278. @d n_ss = 12            {\.{ss}, for the foreign character \.{\\ss}}
  6279. @^important note@>
  6280. @.default.type@>
  6281. Here we pre-define a few strings used in executing the \.{.bst} file:
  6282. the null string, which is sometimes pushed onto the stack; a string
  6283. used for default entry types; and some control sequences used to spot
  6284. foreign characters.  We also initialize the |s_preamble| array to
  6285. empty.  These pre-defined strings must all be exactly |longest_pds|
  6286. characters long.
  6287. Important note: These pre-definitions must not have any glitches or
  6288. the program may bomb because the |log_file| hasn't been opened yet,
  6289. and |text_ilk|s should be pre-defined here, not earlier, for
  6290. \.{.bst}-function-execution purposes.
  6291. @<Pre-define certain strings@>=
  6292. pre_define('            ',0,text_ilk);  s_null := hash_text[pre_def_loc];
  6293. fn_type[pre_def_loc] := str_literal;@/
  6294. pre_define('default.type',12,text_ilk); s_default := hash_text[pre_def_loc];
  6295. fn_type[pre_def_loc] := str_literal;@/
  6296. b_default := b_skip;    {this may be changed to the \.{default.type} function}
  6297. preamble_ptr := 0;                      {initialize the |s_preamble| array}
  6298. pre_define('i           ',1,control_seq_ilk);
  6299. ilk_info[pre_def_loc] := n_i;
  6300. pre_define('j           ',1,control_seq_ilk);
  6301. ilk_info[pre_def_loc] := n_j;
  6302. pre_define('oe          ',2,control_seq_ilk);
  6303. ilk_info[pre_def_loc] := n_oe;
  6304. pre_define('OE          ',2,control_seq_ilk);
  6305. ilk_info[pre_def_loc] := n_oe_upper;
  6306. pre_define('ae          ',2,control_seq_ilk);
  6307. ilk_info[pre_def_loc] := n_ae;
  6308. pre_define('AE          ',2,control_seq_ilk);
  6309. ilk_info[pre_def_loc] := n_ae_upper;
  6310. pre_define('aa          ',2,control_seq_ilk);
  6311. ilk_info[pre_def_loc] := n_aa;
  6312. pre_define('AA          ',2,control_seq_ilk);
  6313. ilk_info[pre_def_loc] := n_aa_upper;
  6314. pre_define('o           ',1,control_seq_ilk);
  6315. ilk_info[pre_def_loc] := n_o;
  6316. pre_define('O           ',1,control_seq_ilk);
  6317. ilk_info[pre_def_loc] := n_o_upper;
  6318. pre_define('l           ',1,control_seq_ilk);
  6319. ilk_info[pre_def_loc] := n_l;
  6320. pre_define('L           ',1,control_seq_ilk);
  6321. ilk_info[pre_def_loc] := n_l_upper;
  6322. pre_define('ss          ',2,control_seq_ilk);
  6323. ilk_info[pre_def_loc] := n_ss;
  6324. @^important note@>
  6325. @.crossref@>
  6326. @.entry.max\$@>
  6327. @.global.max\$@>
  6328. @.sort.key\$@>
  6329. Now we pre-define any built-in |field|s, |str_entry_var|s, and
  6330. |int_global_var|s; these strings must all be exactly |longest_pds|
  6331. characters long.  Note that although these are built-in functions, we
  6332. classify them (in the |fn_type| array) otherwise.
  6333. Important note: These pre-definitions must not have any glitches or
  6334. the program may bomb because the |log_file| hasn't been opened yet.
  6335. @<Pre-define certain strings@>=
  6336. pre_define('crossref    ',8,bst_fn_ilk);
  6337. fn_type[pre_def_loc] := field;@/
  6338. fn_info[pre_def_loc] := num_fields;     {give this |field| a number}
  6339. crossref_num := num_fields;
  6340. incr(num_fields);@/
  6341. num_pre_defined_fields := num_fields;   {that's it for pre-defined |field|s}
  6342. pre_define('sort.key$   ',9,bst_fn_ilk);
  6343. fn_type[pre_def_loc] := str_entry_var;
  6344. fn_info[pre_def_loc] := num_ent_strs;   {give this |str_entry_var| a number}
  6345. sort_key_num := num_ent_strs;
  6346. incr(num_ent_strs);@/
  6347. pre_define('entry.max$  ',10,bst_fn_ilk);
  6348. fn_type[pre_def_loc] := int_global_var;
  6349. fn_info[pre_def_loc] := ent_str_size;   {initialize this |int_global_var|}
  6350. pre_define('global.max$ ',11,bst_fn_ilk);
  6351. fn_type[pre_def_loc] := int_global_var;
  6352. fn_info[pre_def_loc] := glob_str_size;  {initialize this |int_global_var|}
  6353. @^add a built-in function@>
  6354. @:this can't happen}{\quad Unknown built-in function@>
  6355. This module branches to the code for the appropriate |built_in|
  6356. function.  Only three---{\.{call.type\$}}, {\.{if\$}}, and
  6357. {\.{while\$}}---do a recursive call.
  6358. @<Execute a |built_in| function@>=
  6359. begin
  6360.   stat          {update this function's execution count}
  6361.   incr(execution_count[fn_info[ex_fn_loc]]);
  6362.   tats@/
  6363. case (fn_info[ex_fn_loc]) of
  6364.     n_equals :          x_equals;
  6365.     n_greater_than :    x_greater_than;
  6366.     n_less_than :       x_less_than;
  6367.     n_plus :            x_plus;
  6368.     n_minus :           x_minus;
  6369.     n_concatenate :     x_concatenate;
  6370.     n_gets :            x_gets;
  6371.     n_add_period :      x_add_period;
  6372.     n_call_type :       @<|execute_fn|({\.{call.type\$}})@>;
  6373.     n_change_case :     x_change_case;
  6374.     n_chr_to_int :      x_chr_to_int;
  6375.     n_cite :            x_cite;
  6376.     n_duplicate :       x_duplicate;
  6377.     n_empty :           x_empty;
  6378.     n_format_name :     x_format_name;
  6379.     n_if :              @<|execute_fn|({\.{if\$}})@>;
  6380.     n_int_to_chr :      x_int_to_chr;
  6381.     n_int_to_str :      x_int_to_str;
  6382.     n_missing :         x_missing;
  6383.     n_newline :         @<|execute_fn|({\.{newline\$}})@>;
  6384.     n_num_names :       x_num_names;
  6385.     n_pop :             @<|execute_fn|({\.{pop\$}})@>;
  6386.     n_preamble :        x_preamble;
  6387.     n_purify :          x_purify;
  6388.     n_quote :           x_quote;
  6389.     n_skip :            @<|execute_fn|({\.{skip\$}})@>;
  6390.     n_stack :           @<|execute_fn|({\.{stack\$}})@>;
  6391.     n_substring :       x_substring;
  6392.     n_swap :            x_swap;
  6393.     n_text_length :     x_text_length;
  6394.     n_text_prefix :     x_text_prefix;
  6395.     n_top_stack :       @<|execute_fn|({\.{top\$}})@>;
  6396.     n_type :            x_type;
  6397.     n_warning :         x_warning;
  6398.     n_while :           @<|execute_fn|({\.{while\$}})@>;
  6399.     n_width :           x_width;
  6400.     n_write :           x_write;
  6401.     othercases confusion ('Unknown built-in function')
  6402. endcases;
  6403. @^add a built-in function@>
  6404. @^gymnastics@>
  6405. This extra level of module-pointing allows a uniformity of module
  6406. names for the |built_in| functions, regardless of whether they do a
  6407. recursive call to |execute_fn| or are trivial (a single statement).
  6408. Those that do a recursive call are left as part of |execute_fn|,
  6409. avoiding \PASCAL's forward procedure mechanism, and those that don't
  6410. (except for the single-statement ones) are made into procedures so
  6411. that |execute_fn| doesn't get too large.
  6412. @<Procedures and functions for style-file function execution@>=
  6413. @<|execute_fn|({\.{=}})@>@;
  6414. @<|execute_fn|({\.{>}})@>@;
  6415. @<|execute_fn|({\.{<}})@>@;
  6416. @<|execute_fn|({\.{+}})@>@;
  6417. @<|execute_fn|({\.{-}})@>@;
  6418. @<|execute_fn|({\.{*}})@>@;
  6419. @<|execute_fn|({\.{:=}})@>@;
  6420. @<|execute_fn|({\.{add.period\$}})@>@;
  6421. @<|execute_fn|({\.{change.case\$}})@>@;
  6422. @<|execute_fn|({\.{chr.to.int\$}})@>@;
  6423. @<|execute_fn|({\.{cite\$}})@>@;
  6424. @<|execute_fn|({\.{duplicate\$}})@>@;
  6425. @<|execute_fn|({\.{empty\$}})@>@;
  6426. @<|execute_fn|({\.{format.name\$}})@>@;
  6427. @<|execute_fn|({\.{int.to.chr\$}})@>@;
  6428. @<|execute_fn|({\.{int.to.str\$}})@>@;
  6429. @<|execute_fn|({\.{missing\$}})@>@;
  6430. @<|execute_fn|({\.{num.names\$}})@>@;
  6431. @<|execute_fn|({\.{preamble\$}})@>@;
  6432. @<|execute_fn|({\.{purify\$}})@>@;
  6433. @<|execute_fn|({\.{quote\$}})@>@;
  6434. @<|execute_fn|({\.{substring\$}})@>@;
  6435. @<|execute_fn|({\.{swap\$}})@>@;
  6436. @<|execute_fn|({\.{text.length\$}})@>@;
  6437. @<|execute_fn|({\.{text.prefix\$}})@>@;
  6438. @<|execute_fn|({\.{type\$}})@>@;
  6439. @<|execute_fn|({\.{warning\$}})@>@;
  6440. @<|execute_fn|({\.{width\$}})@>@;
  6441. @<|execute_fn|({\.{write\$}})@>@;
  6442. @<|execute_fn| itself@>
  6443. Now it's time to declare some things for executing |built_in|
  6444. functions only.  These (and only these) variables are used
  6445. recursively, so they can't be global.
  6446. @d end_while = 51       {stop executing the \.{while\$} function}
  6447. @<Declarations for executing |built_in| functions@>=
  6448. label end_while;
  6449. var r_pop_lt1,@!r_pop_lt2 : integer;    {stack literals for \.{while\$}}
  6450. @!r_pop_tp1,@!r_pop_tp2 : stk_type;     {stack types for \.{while\$}}
  6451. These are nonrecursive variables that |execute_fn| uses.  Declaring
  6452. them here (instead of in the previous module) saves execution time and
  6453. stack space on most machines.
  6454. @d name_buf == sv_buffer        {an alias, a buffer for manipulating names}
  6455. @<Globals in the outer block@>=
  6456. @!pop_lit1,@!pop_lit2,@!pop_lit3 : integer;     {stack literals}
  6457. @!pop_typ1,@!pop_typ2,@!pop_typ3 : stk_type;    {stack types}
  6458. @!sp_ptr : pool_pointer;                {for manipulating |str_pool| strings}
  6459. @!sp_xptr1,@!sp_xptr2 : pool_pointer;   {more of the same}
  6460. @!sp_end : pool_pointer;                {marks the end of a |str_pool| string}
  6461. @!sp_length,sp2_length : pool_pointer;  {lengths of |str_pool| strings}
  6462. @!sp_brace_level : integer;             {for scanning |str_pool| strings}
  6463. @!ex_buf_xptr,@!ex_buf_yptr : buf_pointer;      {extra |ex_buf| locations}
  6464. @!control_seq_loc : hash_loc;   {hash-table loc of a control sequence}
  6465. @!preceding_white : boolean;    {used in scanning strings}
  6466. @!and_found : boolean;          {to stop the loop that looks for an ``and''}
  6467. @!num_names : integer;          {for counting names}
  6468. @!name_bf_ptr : buf_pointer;    {general |name_buf| location}
  6469. @!name_bf_xptr,@!name_bf_yptr : buf_pointer;    {and two more}
  6470. @!nm_brace_level : integer;     {for scanning |name_buf| strings}
  6471. @!name_tok : packed array[buf_pointer] of buf_pointer; {name-token ptr list}
  6472. @!name_sep_char : packed array[buf_pointer] of ASCII_code; {token-ending chars}
  6473. @!num_tokens : buf_pointer;     {this counts name tokens}
  6474. @!token_starting : boolean;     {used in scanning name tokens}
  6475. @!alpha_found : boolean;        {used in scanning the format string}
  6476. @!double_letter,@!end_of_group,@!to_be_written : boolean;       {the same}
  6477. @!first_start : buf_pointer;    {start-ptr into |name_tok| for the first name}
  6478. @!first_end : buf_pointer;      {end-ptr into |name_tok| for the first name}
  6479. @!last_end : buf_pointer;       {end-ptr into |name_tok| for the last name}
  6480. @!von_start : buf_pointer;      {start-ptr into |name_tok| for the von name}
  6481. @!von_end : buf_pointer;        {end-ptr into |name_tok| for the von name}
  6482. @!jr_end : buf_pointer;         {end-ptr into |name_tok| for the jr name}
  6483. @!cur_token,@!last_token : buf_pointer; {|name_tok| ptrs for outputting tokens}
  6484. @!use_default : boolean;        {for the inter-token intra-name part string}
  6485. @!num_commas : buf_pointer;     {used to determine the name syntax}
  6486. @!comma1,@!comma2 : buf_pointer;        {ptrs into |name_tok|}
  6487. @!num_text_chars : buf_pointer; {special characters count as one}
  6488. The |built_in| function {\.{=}} pops the top two (integer or string)
  6489. literals, compares them, and pushes the integer 1 if they're equal, 0
  6490. otherwise.  If they're not either both string or both integer, it
  6491. complains and pushes the integer 0.
  6492. @<|execute_fn|({\.{=}})@>=
  6493. procedure x_equals;
  6494. begin
  6495. pop_lit_stk (pop_lit1,pop_typ1);
  6496. pop_lit_stk (pop_lit2,pop_typ2);
  6497. if (pop_typ1 <> pop_typ2) then
  6498.     begin
  6499.     if ((pop_typ1 <> stk_empty) and (pop_typ2 <> stk_empty)) then
  6500.         begin
  6501.         print_stk_lit (pop_lit1,pop_typ1);
  6502.         print (', ');
  6503.         print_stk_lit (pop_lit2,pop_typ2);
  6504.         print_newline;
  6505.         bst_ex_warn ('---they aren''t the same literal types');
  6506.         end;
  6507.     push_lit_stk (0, stk_int);
  6508.     end
  6509. else if ((pop_typ1 <> stk_int) and (pop_typ1 <> stk_str)) then
  6510.     begin
  6511.     if (pop_typ1 <> stk_empty) then
  6512.         begin
  6513.         print_stk_lit (pop_lit1,pop_typ1);
  6514.         bst_ex_warn (', not an integer or a string,');
  6515.         end;
  6516.     push_lit_stk (0, stk_int);
  6517.     end
  6518. else if (pop_typ1 = stk_int) then
  6519.     if (pop_lit2 = pop_lit1) then
  6520.         push_lit_stk (1, stk_int)
  6521.       else
  6522.         push_lit_stk (0, stk_int)
  6523.     if (str_eq_str (pop_lit2,pop_lit1)) then
  6524.         push_lit_stk (1, stk_int)
  6525.       else
  6526.         push_lit_stk (0, stk_int);
  6527. The |built_in| function {\.{>}} pops the top two (integer) literals,
  6528. compares them, and pushes the integer 1 if the second is greater than
  6529. the first, 0 otherwise.  If either isn't an integer literal, it
  6530. complains and pushes the integer 0.
  6531. @<|execute_fn|({\.{>}})@>=
  6532. procedure x_greater_than;
  6533. begin
  6534. pop_lit_stk (pop_lit1,pop_typ1);
  6535. pop_lit_stk (pop_lit2,pop_typ2);
  6536. if (pop_typ1 <> stk_int) then
  6537.     begin
  6538.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_int);
  6539.     push_lit_stk (0, stk_int);
  6540.     end
  6541. else if (pop_typ2 <> stk_int) then
  6542.     begin
  6543.     print_wrong_stk_lit (pop_lit2,pop_typ2,stk_int);
  6544.     push_lit_stk (0, stk_int);
  6545.     end
  6546.     if (pop_lit2 > pop_lit1) then
  6547.         push_lit_stk (1, stk_int)
  6548.       else
  6549.         push_lit_stk (0, stk_int);
  6550. The |built_in| function {\.{<}} pops the top two (integer) literals,
  6551. compares them, and pushes the integer 1 if the second is less than the
  6552. first, 0 otherwise.  If either isn't an integer literal, it complains
  6553. and pushes the integer 0.
  6554. @<|execute_fn|({\.{<}})@>=
  6555. procedure x_less_than;
  6556. begin
  6557. pop_lit_stk (pop_lit1,pop_typ1);
  6558. pop_lit_stk (pop_lit2,pop_typ2);
  6559. if (pop_typ1 <> stk_int) then
  6560.     begin
  6561.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_int);
  6562.     push_lit_stk (0, stk_int);
  6563.     end
  6564. else if (pop_typ2 <> stk_int) then
  6565.     begin
  6566.     print_wrong_stk_lit (pop_lit2,pop_typ2,stk_int);
  6567.     push_lit_stk (0, stk_int);
  6568.     end
  6569.     if (pop_lit2 < pop_lit1) then
  6570.         push_lit_stk (1, stk_int)
  6571.       else
  6572.         push_lit_stk (0, stk_int);
  6573. The |built_in| function {\.{+}} pops the top two (integer) literals
  6574. and pushes their sum.  If either isn't an integer literal, it
  6575. complains and pushes the integer 0.
  6576. @<|execute_fn|({\.{+}})@>=
  6577. procedure x_plus;
  6578. begin
  6579. pop_lit_stk (pop_lit1,pop_typ1);
  6580. pop_lit_stk (pop_lit2,pop_typ2);
  6581. if (pop_typ1 <> stk_int) then
  6582.     begin
  6583.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_int);
  6584.     push_lit_stk (0, stk_int);
  6585.     end
  6586. else if (pop_typ2 <> stk_int) then
  6587.     begin
  6588.     print_wrong_stk_lit (pop_lit2,pop_typ2,stk_int);
  6589.     push_lit_stk (0, stk_int);
  6590.     end
  6591.     push_lit_stk (pop_lit2+pop_lit1, stk_int);
  6592. The |built_in| function {\.{-}} pops the top two (integer) literals
  6593. and pushes their difference (the first subtracted from the second).
  6594. If either isn't an integer literal, it complains and pushes the
  6595. integer 0.
  6596. @<|execute_fn|({\.{-}})@>=
  6597. procedure x_minus;
  6598. begin
  6599. pop_lit_stk (pop_lit1,pop_typ1);
  6600. pop_lit_stk (pop_lit2,pop_typ2);
  6601. if (pop_typ1 <> stk_int) then
  6602.     begin
  6603.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_int);
  6604.     push_lit_stk (0, stk_int);
  6605.     end
  6606. else if (pop_typ2 <> stk_int) then
  6607.     begin
  6608.     print_wrong_stk_lit (pop_lit2,pop_typ2,stk_int);
  6609.     push_lit_stk (0, stk_int);
  6610.     end
  6611.     push_lit_stk (pop_lit2-pop_lit1, stk_int);
  6612. The |built_in| function {\.{*}} pops the top two (string) literals,
  6613. concatenates them (in reverse order, that is, the order in which
  6614. pushed), and pushes the resulting string back onto the stack.  If
  6615. either isn't a string literal, it complains and pushes the null
  6616. string.
  6617. @<|execute_fn|({\.{*}})@>=
  6618. procedure x_concatenate;
  6619. begin
  6620. pop_lit_stk (pop_lit1,pop_typ1);
  6621. pop_lit_stk (pop_lit2,pop_typ2);
  6622. if (pop_typ1 <> stk_str) then
  6623.     begin
  6624.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str);
  6625.     push_lit_stk (s_null, stk_str);
  6626.     end
  6627. else if (pop_typ2 <> stk_str) then
  6628.     begin
  6629.     print_wrong_stk_lit (pop_lit2,pop_typ2,stk_str);
  6630.     push_lit_stk (s_null, stk_str);
  6631.     end
  6632.     @<Concatenate the two strings and push@>;
  6633. @^push the literal stack@>
  6634. Often both strings will be at the top of the string pool, in which
  6635. case we just move some pointers.  Furthermore, it's worth doing some
  6636. special stuff in case either string is null, since empirically this
  6637. seems to happen about $20\%$ of the time.  In any case, we don't need
  6638. the execution buffer---we simple move the strings around in the string
  6639. pool when necessary.
  6640. @<Concatenate the two strings and push@>=
  6641. begin
  6642. if (pop_lit2 >= cmd_str_ptr) then
  6643.     if (pop_lit1 >= cmd_str_ptr) then
  6644.         begin
  6645.         str_start[pop_lit1] := str_start[pop_lit1+1];
  6646.         unflush_string;
  6647.         incr(lit_stk_ptr);
  6648.         end
  6649.     else if (length(pop_lit2) = 0) then
  6650.         push_lit_stk (pop_lit1, stk_str)
  6651.     else        {|pop_lit2| is nonnull, only |pop_lit1| is below |cmd_str_ptr|}
  6652.         begin
  6653.         pool_ptr := str_start[pop_lit2+1];
  6654.         str_room (length(pop_lit1));
  6655.         sp_ptr := str_start[pop_lit1];
  6656.         sp_end := str_start[pop_lit1+1];
  6657.         while (sp_ptr < sp_end) do
  6658.             begin
  6659.             append_char (str_pool[sp_ptr]);
  6660.             incr(sp_ptr);
  6661.             end;
  6662.         push_lit_stk (make_string, stk_str);    {and push it onto the stack}
  6663.         end
  6664.     @<Concatenate them and push when |pop_lit2 < cmd_str_ptr|@>;
  6665. @^push the literal stack@>
  6666. We simply continue the previous module.
  6667. @<Concatenate them and push when |pop_lit2 < cmd_str_ptr|@>=
  6668. begin
  6669. if (pop_lit1 >= cmd_str_ptr) then
  6670.     if (length(pop_lit2) = 0) then
  6671.         begin
  6672.         unflush_string;
  6673.         lit_stack[lit_stk_ptr] := pop_lit1;
  6674.         incr(lit_stk_ptr);
  6675.         end
  6676.     else if (length(pop_lit1) = 0) then
  6677.         incr(lit_stk_ptr)
  6678.     else        {both strings nonnull, only |pop_lit2| is below |cmd_str_ptr|}
  6679.         begin
  6680.         sp_length := length(pop_lit1);
  6681.         sp2_length := length(pop_lit2);
  6682.         str_room (sp_length + sp2_length);
  6683.         sp_ptr := str_start[pop_lit1+1];
  6684.         sp_end := str_start[pop_lit1];
  6685.         sp_xptr1 := sp_ptr + sp2_length;
  6686.         while (sp_ptr > sp_end) do              {slide up |pop_lit1|}
  6687.             begin
  6688.             decr(sp_ptr);
  6689.             decr(sp_xptr1);
  6690.             str_pool[sp_xptr1] := str_pool[sp_ptr];
  6691.             end;
  6692.         sp_ptr := str_start[pop_lit2];
  6693.         sp_end := str_start[pop_lit2+1];
  6694.         while (sp_ptr < sp_end) do              {slide up |pop_lit2|}
  6695.             begin
  6696.             append_char (str_pool[sp_ptr]);
  6697.             incr(sp_ptr);
  6698.             end;
  6699.         pool_ptr := pool_ptr + sp_length;
  6700.         push_lit_stk (make_string, stk_str);    {and push it onto the stack}
  6701.         end
  6702.     @<Concatenate them and push when |pop_lit1,pop_lit2 < cmd_str_ptr|@>;
  6703. @^push the literal stack@>
  6704. Again, we simply continue the previous module.
  6705. @<Concatenate them and push when |pop_lit1,pop_lit2 < cmd_str_ptr|@>=
  6706. begin
  6707. if (length(pop_lit1) = 0) then
  6708.     incr(lit_stk_ptr)
  6709. else if (length(pop_lit2) = 0) then
  6710.     push_lit_stk (pop_lit1, stk_str)
  6711. else            {both strings are nonnull, and both are below |cmd_str_ptr|}
  6712.     begin
  6713.     str_room (length(pop_lit1) + length(pop_lit2));
  6714.     sp_ptr := str_start[pop_lit2];
  6715.     sp_end := str_start[pop_lit2+1];
  6716.     while (sp_ptr < sp_end) do                  {slide up |pop_lit2|}
  6717.         begin
  6718.         append_char (str_pool[sp_ptr]);
  6719.         incr(sp_ptr);
  6720.         end;
  6721.     sp_ptr := str_start[pop_lit1];
  6722.     sp_end := str_start[pop_lit1+1];
  6723.     while (sp_ptr < sp_end) do                  {slide up |pop_lit1|}
  6724.         begin
  6725.         append_char (str_pool[sp_ptr]);
  6726.         incr(sp_ptr);
  6727.         end;
  6728.     push_lit_stk (make_string, stk_str);        {and push it onto the stack}
  6729.     end;
  6730. The |built_in| function {\.{:=}} pops the top two literals and assigns
  6731. to the first (which must be an |int_entry_var|, a |str_entry_var|, an
  6732. |int_global_var|, or a |str_global_var|) the value of the second;
  6733. it complains if the value isn't of the appropriate type.
  6734. @<|execute_fn|({\.{:=}})@>=
  6735. procedure x_gets;
  6736. begin
  6737. pop_lit_stk (pop_lit1,pop_typ1);
  6738. pop_lit_stk (pop_lit2,pop_typ2);
  6739. if (pop_typ1 <> stk_fn) then
  6740.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_fn)
  6741. else if ((not mess_with_entries) and
  6742.         ((fn_type[pop_lit1] = str_entry_var) or
  6743.          (fn_type[pop_lit1] = int_entry_var))) then
  6744.     bst_cant_mess_with_entries_print
  6745.     case (fn_type[pop_lit1]) of
  6746.         int_entry_var : @<Assign to an |int_entry_var|@>;
  6747.         str_entry_var : @<Assign to a |str_entry_var|@>;
  6748.         int_global_var : @<Assign to an |int_global_var|@>;
  6749.         str_global_var : @<Assign to a |str_global_var|@>;
  6750.         othercases begin
  6751.                    print ('You can''t assign to type ');
  6752.                    print_fn_class (pop_lit1);
  6753.                    bst_ex_warn (', a nonvariable function class');
  6754.                    end
  6755.     endcases;
  6756. This module checks that what we're about to assign is really an
  6757. integer, and then assigns.
  6758. @<Assign to an |int_entry_var|@>=
  6759. if (pop_typ2 <> stk_int) then
  6760.     print_wrong_stk_lit (pop_lit2,pop_typ2,stk_int)
  6761.   else
  6762.     entry_ints[cite_ptr*num_ent_ints+fn_info[pop_lit1]] := pop_lit2
  6763. @.String size exceeded@>
  6764. It's time for a complaint if either of the two (entry or global)
  6765. string lengths is exceeded.
  6766. @d bst_string_size_exceeded(#) == begin
  6767.                                   bst_1print_string_size_exceeded;
  6768.                                   print (#);
  6769.                                   bst_2print_string_size_exceeded;
  6770.                                   end
  6771. @<Procedures and functions for all file I/O, error messages, and such@>=
  6772. procedure bst_1print_string_size_exceeded;
  6773. begin
  6774. print ('Warning--you''ve exceeded ');
  6775. procedure bst_2print_string_size_exceeded;
  6776. begin
  6777. print ('-string-size,');
  6778. bst_mild_ex_warn_print;
  6779. print_ln ('*Please notify the bibstyle designer*');
  6780. @.entry string size exceeded@>
  6781. @:String size exceeded}{\quad entry string size@>
  6782. This module checks that what we're about to assign is really a
  6783. string, and then assigns.
  6784. @<Assign to a |str_entry_var|@>=
  6785. begin
  6786. if (pop_typ2 <> stk_str) then
  6787.     print_wrong_stk_lit (pop_lit2,pop_typ2,stk_str)
  6788.   else
  6789.     begin
  6790.     str_ent_ptr := cite_ptr*num_ent_strs + fn_info[pop_lit1];
  6791.     ent_chr_ptr := 0;
  6792.     sp_ptr := str_start[pop_lit2];
  6793.     sp_xptr1 := str_start[pop_lit2+1];
  6794.     if (sp_xptr1-sp_ptr > ent_str_size) then
  6795.         begin
  6796.         bst_string_size_exceeded (ent_str_size:0,', the entry');
  6797.         sp_xptr1 := sp_ptr + ent_str_size;
  6798.         end;
  6799.     while (sp_ptr < sp_xptr1) do
  6800.         begin                   {copy characters into |entry_strs|}
  6801.         entry_strs[str_ent_ptr][ent_chr_ptr] := str_pool[sp_ptr];
  6802.         incr(ent_chr_ptr);
  6803.         incr(sp_ptr);
  6804.         end;
  6805.     entry_strs[str_ent_ptr][ent_chr_ptr] := end_of_string;
  6806.     end
  6807. This module checks that what we're about to assign is really an
  6808. integer, and then assigns.
  6809. @<Assign to an |int_global_var|@>=
  6810. if (pop_typ2 <> stk_int) then
  6811.     print_wrong_stk_lit (pop_lit2,pop_typ2,stk_int)
  6812.   else
  6813.     fn_info[pop_lit1] := pop_lit2
  6814. @.global string size exceeded@>
  6815. @:String size exceeded}{\quad global string size@>
  6816. This module checks that what we're about to assign is really a
  6817. string, and then assigns.
  6818. @<Assign to a |str_global_var|@>=
  6819. begin
  6820. if (pop_typ2 <> stk_str) then
  6821.     print_wrong_stk_lit (pop_lit2,pop_typ2,stk_str)
  6822.   else
  6823.     begin
  6824.     str_glb_ptr := fn_info[pop_lit1];
  6825.     if (pop_lit2 < cmd_str_ptr) then
  6826.         glb_str_ptr[str_glb_ptr] := pop_lit2
  6827.       else
  6828.         begin
  6829.         glb_str_ptr[str_glb_ptr] := 0;
  6830.         glob_chr_ptr := 0;
  6831.         sp_ptr := str_start[pop_lit2];
  6832.         sp_end := str_start[pop_lit2+1];
  6833.         if (sp_end - sp_ptr > glob_str_size) then
  6834.             begin
  6835.             bst_string_size_exceeded (glob_str_size:0,', the global');
  6836.             sp_end := sp_ptr + glob_str_size;
  6837.             end;
  6838.         while (sp_ptr < sp_end) do
  6839.             begin                       {copy characters into |global_strs|}
  6840.             global_strs[str_glb_ptr][glob_chr_ptr] := str_pool[sp_ptr];
  6841.             incr(glob_chr_ptr);
  6842.             incr(sp_ptr);
  6843.             end;
  6844.         glb_str_end[str_glb_ptr] := glob_chr_ptr;
  6845.         end;
  6846.     end
  6847. The |built_in| function {\.{add.period\$}} pops the top (string)
  6848. literal, adds a |period| to a nonnull string if its last
  6849. non|right_brace| character isn't a |period|, |question_mark|, or
  6850. |exclamation_mark|, and pushes this resulting string back onto the
  6851. stack.  If the literal isn't a string, it complains and pushes the
  6852. null string.
  6853. @<|execute_fn|({\.{add.period\$}})@>=
  6854. procedure x_add_period;
  6855. label loop_exit;
  6856. begin
  6857. pop_lit_stk (pop_lit1,pop_typ1);
  6858. if (pop_typ1 <> stk_str) then
  6859.     begin
  6860.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str);
  6861.     push_lit_stk (s_null, stk_str);
  6862.     end
  6863. else if (length(pop_lit1) = 0) then     {don't add |period| to the null string}
  6864.     push_lit_stk (s_null, stk_str)
  6865.     @<Add the |period|, if necessary, and push@>;
  6866. @^push the literal stack@>
  6867. Here we scan backwards from the end of the string, skipping
  6868. non|right_brace| characters, to see if we have to add the |period|.
  6869. @<Add the |period|, if necessary, and push@>=
  6870. begin
  6871. sp_ptr := str_start[pop_lit1+1];
  6872. sp_end := str_start[pop_lit1];
  6873. while (sp_ptr > sp_end) do                      {find a non|right_brace|}
  6874.     begin
  6875.     decr(sp_ptr);
  6876.     if (str_pool[sp_ptr] <> right_brace) then
  6877.         goto loop_exit;
  6878.     end;
  6879. loop_exit:
  6880. case (str_pool[sp_ptr]) of
  6881.     period,
  6882.     question_mark,
  6883.     exclamation_mark :
  6884.         repush_string;
  6885.     othercases
  6886.         @<Add the |period| (it's necessary) and push@>
  6887. endcases;
  6888. Ok guys, we really have to do it.
  6889. @<Add the |period| (it's necessary) and push@>=
  6890. begin
  6891. if (pop_lit1 < cmd_str_ptr) then
  6892.     begin
  6893.     str_room (length(pop_lit1)+1);
  6894.     sp_ptr := str_start[pop_lit1];
  6895.     sp_end := str_start[pop_lit1+1];
  6896.     while (sp_ptr < sp_end) do          {slide |pop_lit1| atop the string pool}
  6897.         begin
  6898.         append_char (str_pool[sp_ptr]);
  6899.         incr(sp_ptr);
  6900.         end;
  6901.     end
  6902. else                                    {the string is already there}
  6903.     begin
  6904.     pool_ptr := str_start[pop_lit1+1];
  6905.     str_room (1);
  6906.     end;
  6907. append_char (period);
  6908. push_lit_stk (make_string, stk_str);
  6909. The |built_in| function {\.{call.type\$}} executes the function
  6910. specified in |type_list| for this entry unless it's |undefined|, in
  6911. which case it executes the default function \.{default.type} defined
  6912. in the \.{.bst} file, or unless it's |empty|, in which case it does
  6913. nothing.
  6914. @<|execute_fn|({\.{call.type\$}})@>=
  6915. begin
  6916. if (not mess_with_entries) then
  6917.     bst_cant_mess_with_entries_print
  6918.   else
  6919.     if (type_list[cite_ptr] = undefined) then
  6920.         execute_fn (b_default)
  6921.     else if (type_list[cite_ptr] = empty) then
  6922.         do_nothing
  6923.     else
  6924.         execute_fn (type_list[cite_ptr]);
  6925. The |built_in| function {\.{change.case\$}} pops the top two (string)
  6926. literals; it changes the case of the second according to the
  6927. specifications of the first, as follows.  (Note: The word `letters' in
  6928. the next sentence refers only to those at brace-level~0, the top-most
  6929. brace level; no other characters are changed, except perhaps for
  6930. special characters, described shortly.)  If the first literal is the
  6931. string~\.{t}, it converts to lower case all letters except the very
  6932. first character in the string, which it leaves alone, and except the
  6933. first character following any |colon| and then nonnull |white_space|,
  6934. which it also leaves alone; if it's the string~\.{l}, it converts all
  6935. letters to lower case; if it's the string~\.{u}, it converts all
  6936. letters to upper case; and if it's anything else, it complains and
  6937. does no conversion.  It then pushes this resulting string.  If either
  6938. type is incorrect, it complains and pushes the null string; however,
  6939. if both types are correct but the specification string (i.e., the
  6940. first string) isn't one of the legal ones, it merely pushes the second
  6941. back onto the stack, after complaining.  (Another note: It ignores
  6942. case differences in the specification string; for example, the strings
  6943. \.{t} and \.{T} are equivalent for the purposes of this |built_in|
  6944. function.)
  6945. @d ok_pascal_i_give_up = 21
  6946. @<|execute_fn|({\.{change.case\$}})@>=
  6947. procedure x_change_case;
  6948. label ok_pascal_i_give_up;
  6949. begin
  6950. pop_lit_stk (pop_lit1,pop_typ1);
  6951. pop_lit_stk (pop_lit2,pop_typ2);
  6952. if (pop_typ1 <> stk_str) then
  6953.     begin
  6954.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str);
  6955.     push_lit_stk (s_null, stk_str);
  6956.     end
  6957. else if (pop_typ2 <> stk_str) then
  6958.     begin
  6959.     print_wrong_stk_lit (pop_lit2,pop_typ2,stk_str);
  6960.     push_lit_stk (s_null, stk_str);
  6961.     end
  6962.     begin
  6963.     @<Determine the case-conversion type@>;
  6964.     ex_buf_length := 0;
  6965.     add_buf_pool (pop_lit2);
  6966.     @<Perform the case conversion@>;
  6967.     add_pool_buf_and_push;              {push this string onto the stack}
  6968.     end;
  6969. First we define a few variables for case conversion.  The constant
  6970. definitions, to be used in |case| statements, are in order of probable
  6971. frequency.
  6972. @d title_lowers = 0     {representing the string \.{t}}
  6973. @d all_lowers = 1       {representing the string \.{l}}
  6974. @d all_uppers = 2       {representing the string \.{u}}
  6975. @d bad_conversion = 3   {representing any illegal case-conversion string}
  6976. @<Globals in the outer block@>=
  6977. @!conversion_type : 0..bad_conversion;  {the possible cases}
  6978. @!prev_colon : boolean;                 {|true| if just past a |colon|}
  6979. Now we determine which of the three case-conversion types we're
  6980. dealing with: \.{t},~\.{l}, or~\.{u}.
  6981. @<Determine the case-conversion type@>=
  6982. begin
  6983. case (str_pool[str_start[pop_lit1]]) of
  6984.     "t","T" : conversion_type := title_lowers;
  6985.     "l","L" : conversion_type := all_lowers;
  6986.     "u","U" : conversion_type := all_uppers;
  6987.     othercases conversion_type := bad_conversion
  6988. endcases;
  6989. if ((length(pop_lit1) <> 1) or (conversion_type = bad_conversion)) then
  6990.     begin
  6991.     conversion_type := bad_conversion;
  6992.     print_pool_str (pop_lit1);
  6993.     bst_ex_warn (' is an illegal case-conversion string');
  6994.     end;
  6995. This procedure complains if the just-encountered |right_brace| would
  6996. make |brace_level| negative.
  6997. @<Procedures and functions for name-string processing@>=
  6998. procedure decr_brace_level (@!pop_lit_var : str_number);
  6999. begin
  7000. if (brace_level = 0) then
  7001.     braces_unbalanced_complaint (pop_lit_var)
  7002.   else
  7003.     decr(brace_level);
  7004. This complaint often arises because the style designer has to type
  7005. lots of braces.
  7006. @<Procedures and functions for all file I/O, error messages, and such@>=
  7007. procedure braces_unbalanced_complaint (@!pop_lit_var : str_number);
  7008. begin
  7009. print ('Warning--"');
  7010. print_pool_str (pop_lit_var);
  7011. bst_mild_ex_warn ('" isn''t a brace-balanced string');
  7012. This one makes sure that |brace_level=0| (it's called at a point in a
  7013. string where braces must be balanced).
  7014. @<Procedures and functions for name-string processing@>=
  7015. procedure check_brace_level (@!pop_lit_var : str_number);
  7016. begin
  7017. if (brace_level > 0) then
  7018.     braces_unbalanced_complaint (pop_lit_var);
  7019. Here's where we actually go through the string and do the case
  7020. conversion.
  7021. @<Perform the case conversion@>=
  7022. begin
  7023. brace_level := 0;       {this is the top level}
  7024. ex_buf_ptr := 0;        {we start with the string's first character}
  7025. while (ex_buf_ptr < ex_buf_length) do
  7026.     begin
  7027.     if (ex_buf[ex_buf_ptr] = left_brace) then
  7028.         begin
  7029.         incr(brace_level);
  7030.         if (brace_level <> 1) then
  7031.             goto ok_pascal_i_give_up;
  7032.         if (ex_buf_ptr + 4 > ex_buf_length) then
  7033.             goto ok_pascal_i_give_up
  7034.           else if (ex_buf[ex_buf_ptr+1] <> backslash) then
  7035.             goto ok_pascal_i_give_up;
  7036.         if (conversion_type = title_lowers) then
  7037.           if (ex_buf_ptr = 0) then
  7038.             goto ok_pascal_i_give_up
  7039.           else if ((prev_colon) and
  7040.                         (lex_class[ex_buf[ex_buf_ptr-1]] = white_space)) then
  7041.             goto ok_pascal_i_give_up;
  7042.         @<Convert a special character@>;
  7043. ok_pascal_i_give_up:
  7044.         prev_colon := false;
  7045.         end
  7046.     else if (ex_buf[ex_buf_ptr] = right_brace) then
  7047.         begin
  7048.         decr_brace_level (pop_lit2);
  7049.         prev_colon := false;
  7050.         end
  7051.     else
  7052.         if (brace_level = 0) then
  7053.             @<Convert a |brace_level = 0| character@>;
  7054.     incr(ex_buf_ptr);
  7055.     end;
  7056. check_brace_level (pop_lit2);
  7057. @^special character@>
  7058. We're dealing with a special character (usually either an undotted
  7059. `\i' or `\j', or an accent like one in Table~3.1 of the \LaTeX\
  7060. manual, or a foreign character like one in Table~3.2) if the first
  7061. character after the |left_brace| is a |backslash|; the special
  7062. character ends with the matching |right_brace|.  How we handle what's
  7063. in between depends on the special character.  In general, this code
  7064. will do reasonably well if there is other stuff, too, between braces,
  7065. but it doesn't try to do anything special with |colon|s.
  7066. @<Convert a special character@>=
  7067. begin
  7068. incr(ex_buf_ptr);                       {skip over the |left_brace|}
  7069. while ((ex_buf_ptr < ex_buf_length) and (brace_level > 0)) do
  7070.     begin
  7071.     incr(ex_buf_ptr);                   {skip over the |backslash|}
  7072.     ex_buf_xptr := ex_buf_ptr;
  7073.     while ((ex_buf_ptr < ex_buf_length) and
  7074.                 (lex_class[ex_buf[ex_buf_ptr]] = alpha)) do
  7075.         incr(ex_buf_ptr);               {this scans the control sequence}
  7076.     control_seq_loc := str_lookup(ex_buf,ex_buf_xptr,ex_buf_ptr-ex_buf_xptr,
  7077.                                                 control_seq_ilk,dont_insert);
  7078.     if (hash_found) then
  7079.         @<Convert the accented or foreign character, if necessary@>;
  7080.     ex_buf_xptr := ex_buf_ptr;
  7081.     while ((ex_buf_ptr < ex_buf_length) and (brace_level > 0) and
  7082.                                         (ex_buf[ex_buf_ptr] <> backslash)) do
  7083.         begin                   {this scans to the next control sequence}
  7084.         if (ex_buf[ex_buf_ptr] = right_brace) then
  7085.             decr(brace_level)
  7086.         else if (ex_buf[ex_buf_ptr] = left_brace) then
  7087.             incr(brace_level);
  7088.         incr(ex_buf_ptr);
  7089.         end;
  7090.     @<Convert a noncontrol sequence@>;
  7091.     end;
  7092. decr(ex_buf_ptr);               {unskip the |right_brace|}
  7093. @^control sequence@>
  7094. @:this can't happen}{\quad Unknown type of case conversion@>
  7095. A control sequence, for the purposes of this program, consists just of
  7096. the consecutive alphabetic characters following the |backslash|; it
  7097. might be empty (although ones in this section aren't).
  7098. @<Convert the accented or foreign character, if necessary@>=
  7099. begin
  7100. case (conversion_type) of
  7101.     title_lowers,
  7102.     all_lowers :
  7103.         case (ilk_info[control_seq_loc]) of
  7104.             n_l_upper,
  7105.             n_o_upper,
  7106.             n_oe_upper,
  7107.             n_ae_upper,
  7108.             n_aa_upper :
  7109.                 lower_case (ex_buf, ex_buf_xptr, ex_buf_ptr-ex_buf_xptr);
  7110.             othercases
  7111.                 do_nothing
  7112.         endcases;
  7113.     all_uppers :
  7114.         case (ilk_info[control_seq_loc]) of
  7115.             n_l,
  7116.             n_o,
  7117.             n_oe,
  7118.             n_ae,
  7119.             n_aa :
  7120.                 upper_case (ex_buf, ex_buf_xptr, ex_buf_ptr-ex_buf_xptr);
  7121.             n_i,
  7122.             n_j,
  7123.             n_ss :
  7124.                 @<Convert, then remove the control sequence@>;
  7125.             othercases
  7126.                 do_nothing
  7127.         endcases;
  7128.     bad_conversion :
  7129.         do_nothing;
  7130.     othercases
  7131.         case_conversion_confusion
  7132. endcases;
  7133. @:this can't happen}{\quad Unknown type of case conversion@>
  7134. Another bug complaint.
  7135. @<Procedures and functions for all file I/O, error messages, and such@>=
  7136. procedure case_conversion_confusion;
  7137. begin
  7138. confusion ('Unknown type of case conversion');
  7139. After converting the control sequence, we need to remove the preceding
  7140. |backslash| and any following |white_space|.
  7141. @<Convert, then remove the control sequence@>=
  7142. begin
  7143. upper_case (ex_buf, ex_buf_xptr, ex_buf_ptr-ex_buf_xptr);
  7144. while (ex_buf_xptr < ex_buf_ptr) do
  7145.     begin                       {remove preceding |backslash| and shift down}
  7146.     ex_buf[ex_buf_xptr-1] := ex_buf[ex_buf_xptr];
  7147.     incr(ex_buf_xptr);
  7148.     end;
  7149. decr(ex_buf_xptr);
  7150. while ((ex_buf_ptr < ex_buf_length) and
  7151.                 (lex_class[ex_buf[ex_buf_ptr]] = white_space)) do
  7152.     incr(ex_buf_ptr);           {remove |white_space| trailing the control seq}
  7153. tmp_ptr := ex_buf_ptr;
  7154. while (tmp_ptr < ex_buf_length) do
  7155.     begin                       {more shifting down}
  7156.     ex_buf[tmp_ptr-(ex_buf_ptr-ex_buf_xptr)] := ex_buf[tmp_ptr];
  7157.     incr(tmp_ptr)
  7158.     end;
  7159. ex_buf_length := tmp_ptr - (ex_buf_ptr - ex_buf_xptr);
  7160. ex_buf_ptr := ex_buf_xptr;
  7161. @:this can't happen}{\quad Unknown type of case conversion@>
  7162. There are no control sequences in what we're about to convert,
  7163. so a straight conversion suffices.
  7164. @<Convert a noncontrol sequence@>=
  7165. begin
  7166. case (conversion_type) of
  7167.     title_lowers,
  7168.     all_lowers :
  7169.         lower_case (ex_buf, ex_buf_xptr, ex_buf_ptr-ex_buf_xptr);
  7170.     all_uppers :
  7171.         upper_case (ex_buf, ex_buf_xptr, ex_buf_ptr-ex_buf_xptr);
  7172.     bad_conversion :
  7173.         do_nothing;
  7174.     othercases
  7175.         case_conversion_confusion
  7176. endcases;
  7177. @:this can't happen}{\quad Unknown type of case conversion@>
  7178. This code does any needed conversion for an ordinary character; it
  7179. won't touch nonletters.
  7180. @<Convert a |brace_level = 0| character@>=
  7181. begin
  7182. case (conversion_type) of
  7183.     title_lowers :
  7184.         begin
  7185.         if (ex_buf_ptr = 0) then
  7186.             do_nothing
  7187.         else if ((prev_colon) and
  7188.                         (lex_class[ex_buf[ex_buf_ptr-1]] = white_space)) then
  7189.             do_nothing
  7190.         else
  7191.             lower_case (ex_buf, ex_buf_ptr, 1);
  7192.         if (ex_buf[ex_buf_ptr] = colon) then
  7193.             prev_colon := true
  7194.         else if (lex_class[ex_buf[ex_buf_ptr]] <> white_space) then
  7195.             prev_colon := false;
  7196.         end;
  7197.     all_lowers :
  7198.         lower_case (ex_buf, ex_buf_ptr, 1);
  7199.     all_uppers :
  7200.         upper_case (ex_buf, ex_buf_ptr, 1);
  7201.     bad_conversion :
  7202.         do_nothing;
  7203.     othercases
  7204.         case_conversion_confusion
  7205. endcases;
  7206. The |built_in| function {\.{chr.to.int\$}} pops the top (string)
  7207. literal, makes sure it's a single character, converts it to the
  7208. corresponding |ASCII_code| integer, and pushes this integer.  If the
  7209. literal isn't an appropriate string, it complains and pushes the
  7210. integer~0.
  7211. @<|execute_fn|({\.{chr.to.int\$}})@>=
  7212. procedure x_chr_to_int;
  7213. begin
  7214. pop_lit_stk (pop_lit1,pop_typ1);
  7215. if (pop_typ1 <> stk_str) then
  7216.     begin
  7217.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str);
  7218.     push_lit_stk (0, stk_int);
  7219.     end
  7220. else if (length(pop_lit1) <> 1) then
  7221.     begin
  7222.     print ('"');
  7223.     print_pool_str (pop_lit1);
  7224.     bst_ex_warn ('" isn''t a single character');
  7225.     push_lit_stk (0, stk_int);
  7226.     end
  7227.     push_lit_stk (str_pool[str_start[pop_lit1]], stk_int);
  7228.                                         {push the (|ASCII_code|) integer}
  7229. The |built_in| function {\.{cite\$}} pushes the appropriate string
  7230. from |cite_list| onto the stack.
  7231. @<|execute_fn|({\.{cite\$}})@>=
  7232. procedure x_cite;
  7233. begin
  7234. if (not mess_with_entries) then
  7235.     bst_cant_mess_with_entries_print
  7236.   else
  7237.     push_lit_stk (cur_cite_str, stk_str);
  7238. @^push the literal stack@>
  7239. The |built_in| function {\.{duplicate\$}} pops the top literal from
  7240. the stack and pushes two copies of it.
  7241. @<|execute_fn|({\.{duplicate\$}})@>=
  7242. procedure x_duplicate;
  7243. begin
  7244. pop_lit_stk (pop_lit1,pop_typ1);
  7245. if (pop_typ1 <> stk_str) then
  7246.     begin
  7247.     push_lit_stk (pop_lit1, pop_typ1);
  7248.     push_lit_stk (pop_lit1, pop_typ1);
  7249.     end
  7250.   else
  7251.     begin
  7252.     repush_string;
  7253.     if (pop_lit1 < cmd_str_ptr) then
  7254.         push_lit_stk (pop_lit1, pop_typ1)
  7255.       else
  7256.         begin
  7257.         str_room (length(pop_lit1));
  7258.         sp_ptr := str_start[pop_lit1];
  7259.         sp_end := str_start[pop_lit1+1];
  7260.         while (sp_ptr < sp_end) do
  7261.             begin
  7262.             append_char (str_pool[sp_ptr]);
  7263.             incr(sp_ptr);
  7264.             end;
  7265.         push_lit_stk (make_string, stk_str);    {and push it onto the stack}
  7266.         end;
  7267.     end;
  7268. The |built_in| function {\.{empty\$}} pops the top literal and pushes
  7269. the integer 1 if it's a missing field or a string having no
  7270. non|white_space| characters, 0 otherwise.  If the literal isn't a
  7271. missing field or a string, it complains and pushes 0.
  7272. @<|execute_fn|({\.{empty\$}})@>=
  7273. procedure x_empty;
  7274. label exit;
  7275. begin
  7276. pop_lit_stk (pop_lit1,pop_typ1);
  7277. case (pop_typ1) of
  7278.     stk_str : @<Push 0 if the string has a non|white_space| char, else 1@>;
  7279.     stk_field_missing : push_lit_stk (1, stk_int);
  7280.     stk_empty : push_lit_stk (0, stk_int);
  7281.     othercases
  7282.         begin
  7283.         print_stk_lit (pop_lit1,pop_typ1);
  7284.         bst_ex_warn (', not a string or missing field,');
  7285.         push_lit_stk (0, stk_int);
  7286.         end
  7287. endcases;
  7288. exit:
  7289. When we arrive here we're dealing with a legitimate string.  If it has
  7290. no characters, or has nothing but |white_space| characters, we push~1,
  7291. otherwise we push~0.
  7292. @<Push 0 if the string has a non|white_space| char, else 1@>=
  7293. begin
  7294. sp_ptr := str_start[pop_lit1];
  7295. sp_end := str_start[pop_lit1+1];
  7296. while (sp_ptr < sp_end) do
  7297.     begin
  7298.     if (lex_class[str_pool[sp_ptr]] <> white_space) then
  7299.         begin
  7300.         push_lit_stk (0, stk_int);
  7301.         return;
  7302.         end;
  7303.     incr(sp_ptr);
  7304.     end;
  7305. push_lit_stk (1, stk_int);
  7306. The |built_in| function {\.{format.name\$}} pops the top three
  7307. literals (they are a string, an integer, and a string literal, in that
  7308. order).  The last string literal represents a name list (each name
  7309. corresponding to a person), the integer literal specifies which name
  7310. to pick from this list, and the first string literal specifies how to
  7311. format this name, as described in the \BibTeX\ documentation.
  7312. Finally, this function pushes the formatted name.  If any of the types
  7313. is incorrect, it complains and pushes the null string.
  7314. @d von_found = 52               {for when a von token is found}
  7315. @<|execute_fn|({\.{format.name\$}})@>=
  7316. procedure x_format_name;
  7317. label loop1_exit,@!loop2_exit,@!von_found;
  7318. begin
  7319. pop_lit_stk (pop_lit1,pop_typ1);
  7320. pop_lit_stk (pop_lit2,pop_typ2);
  7321. pop_lit_stk (pop_lit3,pop_typ3);
  7322. if (pop_typ1 <> stk_str) then
  7323.     begin
  7324.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str);
  7325.     push_lit_stk (s_null, stk_str);
  7326.     end
  7327. else if (pop_typ2 <> stk_int) then
  7328.     begin
  7329.     print_wrong_stk_lit (pop_lit2,pop_typ2,stk_int);
  7330.     push_lit_stk (s_null, stk_str);
  7331.     end
  7332. else if (pop_typ3 <> stk_str) then
  7333.     begin
  7334.     print_wrong_stk_lit (pop_lit3,pop_typ3,stk_str);
  7335.     push_lit_stk (s_null, stk_str);
  7336.     end
  7337.     begin
  7338.     ex_buf_length := 0;
  7339.     add_buf_pool (pop_lit3);
  7340.     @<Isolate the desired name@>;
  7341.     @<Copy name and count |comma|s to determine syntax@>;
  7342.     @<Find the parts of the name@>;
  7343.     ex_buf_length := 0;
  7344.     add_buf_pool (pop_lit1);
  7345.     figure_out_the_formatted_name;@/
  7346.     add_pool_buf_and_push;      {push the formatted string onto the stack}
  7347.     end;
  7348. This module skips over undesired names in |pop_lit3| and it throws
  7349. away the ``and'' from the end of the name if it exists.  When it's
  7350. done, |ex_buf_xptr| points to its first character and |ex_buf_ptr|
  7351. points just past its last.
  7352. @<Isolate the desired name@>=
  7353. begin
  7354. ex_buf_ptr := 0;
  7355. num_names := 0;
  7356. while ((num_names < pop_lit2) and (ex_buf_ptr < ex_buf_length)) do
  7357.     begin
  7358.     incr(num_names);
  7359.     ex_buf_xptr := ex_buf_ptr;
  7360.     name_scan_for_and (pop_lit3);
  7361.     end;
  7362. if (ex_buf_ptr < ex_buf_length) then            {remove the ``and''}
  7363.     ex_buf_ptr := ex_buf_ptr - 4;
  7364. if (num_names < pop_lit2) then
  7365.     begin
  7366.     if (pop_lit2 = 1) then
  7367.         print ('There is no name in "')
  7368.       else
  7369.         print ('There aren''t ',pop_lit2:0,' names in "');
  7370.     print_pool_str (pop_lit3);
  7371.     bst_ex_warn ('"');
  7372.     end
  7373. This module, starting at |ex_buf_ptr|, looks in |ex_buf| for an
  7374. ``and'' surrounded by nonnull |white_space|.  It stops either at
  7375. |ex_buf_length| or just past the ``and'', whichever comes first,
  7376. setting |ex_buf_ptr| accordingly.  Its parameter |pop_lit_var| is
  7377. either |pop_lit3| or |pop_lit1|, depending on whether
  7378. {\.{format.name\$}} or {\.{num.names\$}} calls it.
  7379. @<Procedures and functions for name-string processing@>=
  7380. procedure name_scan_for_and (@!pop_lit_var : str_number);
  7381. begin
  7382. brace_level := 0;
  7383. preceding_white := false;
  7384. and_found := false;
  7385. while ((not and_found) and (ex_buf_ptr < ex_buf_length)) do
  7386.   case (ex_buf[ex_buf_ptr]) of
  7387.     "a", "A" :
  7388.         begin
  7389.         incr(ex_buf_ptr);
  7390.         if (preceding_white) then
  7391.             @<See if we have an ``and''@>;      {if so, |and_found := true|}
  7392.         preceding_white := false;
  7393.         end;
  7394.     left_brace :
  7395.         begin
  7396.         incr(brace_level);
  7397.         incr(ex_buf_ptr);
  7398.         @<Skip over |ex_buf| stuff at |brace_level > 0|@>;
  7399.         preceding_white := false;
  7400.         end;
  7401.     right_brace :
  7402.         begin
  7403.         decr_brace_level (pop_lit_var);         {this checks for an error}
  7404.         incr(ex_buf_ptr);
  7405.         preceding_white := false;
  7406.         end;
  7407.     othercases
  7408.         if (lex_class[ex_buf[ex_buf_ptr]] = white_space) then
  7409.             begin
  7410.             incr(ex_buf_ptr);
  7411.             preceding_white := true;
  7412.             end
  7413.         else
  7414.             begin
  7415.             incr(ex_buf_ptr);
  7416.             preceding_white := false;
  7417.             end
  7418.   endcases;
  7419. check_brace_level (pop_lit_var);
  7420. When we come here |ex_buf_ptr| is just past the |left_brace|, and when
  7421. we leave it's either at |ex_buf_length| or just past the matching
  7422. |right_brace|.
  7423. @<Skip over |ex_buf| stuff at |brace_level > 0|@>=
  7424. while ((brace_level > 0) and (ex_buf_ptr < ex_buf_length)) do
  7425.     begin
  7426.     if (ex_buf[ex_buf_ptr] = right_brace) then
  7427.         decr(brace_level)
  7428.     else if (ex_buf[ex_buf_ptr] = left_brace) then
  7429.         incr(brace_level);
  7430.     incr(ex_buf_ptr);
  7431.     end
  7432. When we come here |ex_buf_ptr| is just past the ``a'' or ``A'', and when
  7433. we leave it's either at the same place or, if we found an ``and'', at
  7434. the following |white_space| character.
  7435. @<See if we have an ``and''@>=
  7436. begin
  7437. if (ex_buf_ptr <= (ex_buf_length - 3)) then     {enough characters are left}
  7438.     if ((ex_buf[ex_buf_ptr] = "n") or (ex_buf[ex_buf_ptr] = "N")) then
  7439.         if ((ex_buf[ex_buf_ptr+1] = "d") or (ex_buf[ex_buf_ptr+1] = "D")) then
  7440.             if (lex_class[ex_buf[ex_buf_ptr+2]] = white_space) then
  7441.                 begin
  7442.                 ex_buf_ptr := ex_buf_ptr + 2;
  7443.                 and_found := true;
  7444.                 end;
  7445. When we arrive here, the desired name is in |ex_buf[ex_buf_xptr]|
  7446. through |ex_buf[ex_buf_ptr-1]|.  This module does its thing for
  7447. characters only at |brace_level = 0|; the rest get processed verbatim.
  7448. It removes leading |white_space| (and |sep_char|s), and trailing
  7449. |white_space| (and |sep_char|s) and |comma|s, complaining for each
  7450. trailing |comma|.  It then copies the name into |name_buf|, removing
  7451. all |white_space|, |sep_char|s and |comma|s, counting |comma|s, and
  7452. constructing a list of name tokens, which are sequences of characters
  7453. separated (at |brace_level=0|) by |white_space|, |sep_char|s or
  7454. |comma|s.  Each name token but the first has an associated
  7455. |name_sep_char|, the character that separates it from the preceding
  7456. token.  If there are too many (more than two) |comma|s, a complaint is
  7457. in order.
  7458. @<Copy name and count |comma|s to determine syntax@>=
  7459. begin
  7460. @<Remove leading and trailing junk, complaining if necessary@>;
  7461. name_bf_ptr := 0;
  7462. num_commas := 0;
  7463. num_tokens := 0;@/
  7464. token_starting := true;         {to indicate that a name token is starting}
  7465. while (ex_buf_xptr < ex_buf_ptr) do
  7466.     case (ex_buf[ex_buf_xptr]) of
  7467.         comma : @<Name-process a |comma|@>;
  7468.         left_brace : @<Name-process a |left_brace|@>;
  7469.         right_brace : @<Name-process a |right_brace|@>;
  7470.         othercases
  7471.             case (lex_class[ex_buf[ex_buf_xptr]]) of
  7472.                 white_space : @<Name-process a |white_space|@>;
  7473.                 sep_char : @<Name-process a |sep_char|@>;
  7474.                 othercases @<Name-process some other character@>
  7475.             endcases
  7476.     endcases;
  7477. name_tok[num_tokens] := name_bf_ptr;    {this is an end-marker}
  7478. This module removes all leading |white_space| (and |sep_char|s), and
  7479. trailing |white_space| (and |sep_char|s) and |comma|s.  It complains
  7480. for each trailing |comma|.
  7481. @<Remove leading and trailing junk, complaining if necessary@>=
  7482. begin
  7483. while ((ex_buf_xptr < ex_buf_ptr) and
  7484.                         (lex_class[ex_buf[ex_buf_ptr]] = white_space) and
  7485.                         (lex_class[ex_buf[ex_buf_ptr]] = sep_char)) do
  7486.         incr(ex_buf_xptr);                      {this removes leading stuff}
  7487. while (ex_buf_ptr > ex_buf_xptr) do             {now remove trailing stuff}
  7488.     case (lex_class[ex_buf[ex_buf_ptr-1]]) of
  7489.         white_space,
  7490.         sep_char :
  7491.             decr(ex_buf_ptr);
  7492.         othercases
  7493.             if (ex_buf[ex_buf_ptr-1] = comma) then
  7494.                 begin
  7495.                 print ('Name ',pop_lit2:0,' in "');
  7496.                 print_pool_str (pop_lit3);
  7497.                 print ('" has a comma at the end');
  7498.                 bst_ex_warn_print;
  7499.                 decr(ex_buf_ptr);
  7500.                 end
  7501.             else
  7502.                 goto loop1_exit
  7503.     endcases;
  7504. loop1_exit:
  7505. Here we mark the token number at which this comma has occurred.
  7506. @<Name-process a |comma|@>=
  7507. begin
  7508. if (num_commas = 2) then
  7509.     begin
  7510.     print ('Too many commas in name ',pop_lit2:0,' of "');
  7511.     print_pool_str (pop_lit3);
  7512.     print ('"');
  7513.     bst_ex_warn_print;
  7514.     end
  7515.   else
  7516.     begin
  7517.     incr(num_commas);
  7518.     if (num_commas = 1) then
  7519.         comma1 := num_tokens
  7520.       else
  7521.         comma2 := num_tokens;                   {|num_commas = 2|}
  7522.     name_sep_char[num_tokens] := comma;
  7523.     end;
  7524. incr(ex_buf_xptr);
  7525. token_starting := true;
  7526. We copy the stuff up through the matching |right_brace| verbatim.
  7527. @<Name-process a |left_brace|@>=
  7528. begin
  7529. incr(brace_level);
  7530. if (token_starting) then
  7531.     begin
  7532.     name_tok[num_tokens] := name_bf_ptr;
  7533.     incr(num_tokens);
  7534.     end;
  7535. name_buf[name_bf_ptr] := ex_buf[ex_buf_xptr];
  7536. incr(name_bf_ptr);
  7537. incr(ex_buf_xptr);
  7538. while ((brace_level > 0) and (ex_buf_xptr < ex_buf_ptr)) do
  7539.     begin
  7540.     if (ex_buf[ex_buf_xptr] = right_brace) then
  7541.         decr(brace_level)
  7542.     else if (ex_buf[ex_buf_xptr] = left_brace) then
  7543.         incr(brace_level);
  7544.     name_buf[name_bf_ptr] := ex_buf[ex_buf_xptr];
  7545.     incr(name_bf_ptr);
  7546.     incr(ex_buf_xptr);
  7547.     end;
  7548. token_starting := false;
  7549. We don't copy an extra |right_brace|; this code will almost never be
  7550. executed.
  7551. @<Name-process a |right_brace|@>=
  7552. begin
  7553. if (token_starting) then
  7554.     begin
  7555.     name_tok[num_tokens] := name_bf_ptr;
  7556.     incr(num_tokens);
  7557.     end;
  7558. print ('Name ',pop_lit2:0,' of "');
  7559. print_pool_str (pop_lit3);
  7560. bst_ex_warn ('" isn''t brace balanced');
  7561. incr(ex_buf_xptr);
  7562. token_starting := false;
  7563. A token will be starting soon in a buffer near you, one way$\ldots$
  7564. @<Name-process a |white_space|@>=
  7565. begin
  7566. if (not token_starting) then
  7567.     name_sep_char[num_tokens] := space;
  7568. incr(ex_buf_xptr);
  7569. token_starting := true;
  7570. @^user abuse@>
  7571. or another.  If one of the valid |sep_char|s appears between tokens,
  7572. we usually use it instead of a |space|.  If the user has been silly
  7573. enough to have multiple |sep_char|s, or to have both |white_space| and
  7574. a |sep_char|, we use the first such character.
  7575. @<Name-process a |sep_char|@>=
  7576. begin
  7577. if (not token_starting) then
  7578.     name_sep_char[num_tokens] := ex_buf[ex_buf_xptr];
  7579. incr(ex_buf_xptr);
  7580. token_starting := true;
  7581. For ordinary characters, we just copy the character.
  7582. @<Name-process some other character@>=
  7583. begin
  7584. if (token_starting) then
  7585.     begin
  7586.     name_tok[num_tokens] := name_bf_ptr;
  7587.     incr(num_tokens);
  7588.     end;
  7589. name_buf[name_bf_ptr] := ex_buf[ex_buf_xptr];
  7590. incr(name_bf_ptr);
  7591. incr(ex_buf_xptr);
  7592. token_starting := false;
  7593. @:this can't happen}{\quad Illegal number of comma,s@>
  7594. Here we set all the pointers for the various parts of the name,
  7595. depending on which of the three possible syntaxes this name uses.
  7596. @<Find the parts of the name@>=
  7597. begin
  7598. if (num_commas = 0) then
  7599.     begin
  7600.     first_start := 0;
  7601.     last_end := num_tokens;
  7602.     jr_end := last_end;
  7603.     @<Determine where the first name ends and von name starts and ends@>;
  7604.     end
  7605. else if (num_commas = 1) then
  7606.     begin
  7607.     von_start := 0;
  7608.     last_end := comma1;
  7609.     jr_end := last_end;
  7610.     first_start := jr_end;
  7611.     first_end := num_tokens;
  7612.     von_name_ends_and_last_name_starts_stuff;
  7613.     end
  7614. else if (num_commas = 2) then
  7615.     begin
  7616.     von_start := 0;
  7617.     last_end := comma1;
  7618.     jr_end := comma2;
  7619.     first_start := jr_end;
  7620.     first_end := num_tokens;
  7621.     von_name_ends_and_last_name_starts_stuff;
  7622.     end
  7623.     confusion ('Illegal number of comma,s');
  7624. When there are no brace-level-0 |comma|s in the name, the von name
  7625. starts with the first nonlast token whose first brace-level-0 letter
  7626. is in lower case (for the purposes of this determination, an accented
  7627. or foreign character at brace-level-1 that's in lower case will do, as
  7628. well).  A module following this one determines where the von name ends
  7629. and the last starts.
  7630. @<Determine where the first name ends and von name starts and ends@>=
  7631. begin
  7632. von_start := 0;
  7633. while (von_start < last_end-1) do
  7634.     begin
  7635.     name_bf_ptr := name_tok[von_start];
  7636.     name_bf_xptr := name_tok[von_start+1];
  7637.     if (von_token_found) then
  7638.         begin
  7639.         von_name_ends_and_last_name_starts_stuff;
  7640.         goto von_found;
  7641.         end;
  7642.     incr(von_start);
  7643.     end;                        {there's no von name, so}
  7644. while (von_start > 0) do        {backtrack if there are connected tokens}
  7645.     begin
  7646.     if ((lex_class[name_sep_char[von_start]] <> sep_char) or
  7647.                         (name_sep_char[von_start] = tie)) then
  7648.         goto loop2_exit;
  7649.     decr(von_start);
  7650.     end;
  7651. loop2_exit:
  7652. von_end := von_start;
  7653. von_found:
  7654. first_end := von_start;
  7655. @^special character@>
  7656. It's a von token if there exists a first brace-level-0 letter (or
  7657. brace-level-1 special character), and it's in lower case; in this case
  7658. we return |true|.  The token is in |name_buf|, starting at
  7659. |name_bf_ptr| and ending just before |name_bf_xptr|.
  7660. @d return_von_found ==  begin
  7661.                         von_token_found := true;
  7662.                         return;
  7663.                         end
  7664. @<Procedures and functions for name-string processing@>=
  7665. function von_token_found : boolean;
  7666. label exit;
  7667. begin
  7668. nm_brace_level := 0;
  7669. von_token_found := false;               {now it's easy to exit if necessary}
  7670. while (name_bf_ptr < name_bf_xptr) do
  7671.     if ((name_buf[name_bf_ptr] >= "A") and
  7672.                         (name_buf[name_bf_ptr] <= "Z")) then
  7673.         return
  7674.     else if ((name_buf[name_bf_ptr] >= "a") and
  7675.                         (name_buf[name_bf_ptr] <= "z")) then
  7676.         return_von_found
  7677.     else if (name_buf[name_bf_ptr] = left_brace) then
  7678.         begin
  7679.         incr(nm_brace_level);
  7680.         incr(name_bf_ptr);
  7681.         if ((name_bf_ptr + 2 < name_bf_xptr) and
  7682.                                 (name_buf[name_bf_ptr] = backslash)) then
  7683.             @<Check the special character (and |return|)@>
  7684.           else
  7685.             @<Skip over |name_buf| stuff at |nm_brace_level > 0|@>;
  7686.         end
  7687.     else
  7688.         incr(name_bf_ptr);
  7689. exit:
  7690. @^special character@>
  7691. When we come here |name_bf_ptr| is just past the |left_brace|,
  7692. but we always leave by |return|ing.
  7693. @<Check the special character (and |return|)@>=
  7694. begin
  7695. incr(name_bf_ptr);                      {skip over the |backslash|}
  7696. name_bf_yptr := name_bf_ptr;
  7697. while ((name_bf_ptr < name_bf_xptr) and
  7698.                 (lex_class[name_buf[name_bf_ptr]] = alpha)) do
  7699.     incr(name_bf_ptr);                  {this scans the control sequence}
  7700. control_seq_loc := str_lookup(name_buf,name_bf_yptr,name_bf_ptr-name_bf_yptr,
  7701.                                                 control_seq_ilk,dont_insert);
  7702. if (hash_found) then
  7703.     @<Handle this accented or foreign character (and |return|)@>;
  7704. while ((name_bf_ptr < name_bf_xptr) and (nm_brace_level > 0)) do
  7705.     begin
  7706.     if ((name_buf[name_bf_ptr] >= "A") and
  7707.                         (name_buf[name_bf_ptr] <= "Z")) then
  7708.         return
  7709.     else if ((name_buf[name_bf_ptr] >= "a") and
  7710.                         (name_buf[name_bf_ptr] <= "z")) then
  7711.         return_von_found
  7712.     else if (name_buf[name_bf_ptr] = right_brace) then
  7713.         decr(nm_brace_level)
  7714.     else if (name_buf[name_bf_ptr] = left_brace) then
  7715.         incr(nm_brace_level);
  7716.     incr(name_bf_ptr);
  7717.     end;
  7718. return;
  7719. @:this can't happen}{\quad Control-sequence hash error@>
  7720. The accented or foreign character is either `\.{\\i}' or `\.{\\j}' or
  7721. one of the eleven alphabetic foreign characters in Table~3.2 of the
  7722. \LaTeX\ manual.
  7723. @<Handle this accented or foreign character (and |return|)@>=
  7724. begin
  7725. case (ilk_info[control_seq_loc]) of
  7726.     n_oe_upper,
  7727.     n_ae_upper,
  7728.     n_aa_upper,
  7729.     n_o_upper,
  7730.     n_l_upper :
  7731.         return;
  7732.     n_i,
  7733.     n_j,
  7734.     n_oe,
  7735.     n_ae,
  7736.     n_aa,
  7737.     n_o,
  7738.     n_l,
  7739.     n_ss :
  7740.         return_von_found;
  7741.     othercases
  7742.         confusion ('Control-sequence hash error')
  7743. endcases;
  7744. When we come here |name_bf_ptr| is just past the |left_brace|; when we
  7745. leave it's either at |name_bf_xptr| or just past the matching
  7746. |right_brace|.
  7747. @<Skip over |name_buf| stuff at |nm_brace_level > 0|@>=
  7748. while ((nm_brace_level > 0) and (name_bf_ptr < name_bf_xptr)) do
  7749.     begin
  7750.     if (name_buf[name_bf_ptr] = right_brace) then
  7751.         decr(nm_brace_level)
  7752.     else if (name_buf[name_bf_ptr] = left_brace) then
  7753.         incr(nm_brace_level);
  7754.     incr(name_bf_ptr);
  7755.     end
  7756. @^Casey Stengel would be proud@>
  7757. @^special character@>
  7758. @^Tuesdays@>
  7759. The last name starts just past the last token, before the first
  7760. |comma| (if there is no |comma|, there is deemed to be one at the end
  7761. of the string), for which there exists a first brace-level-0 letter
  7762. (or brace-level-1 special character), and it's in lower case, unless
  7763. this last token is also the last token before the |comma|, in which
  7764. case the last name starts with this token (unless this last token is
  7765. connected by a |sep_char| other than a |tie| to the previous token, in
  7766. which case the last name starts with as many tokens earlier as are
  7767. connected by non|tie|s to this last one (except on Tuesdays
  7768. $\ldots\,$), although this module never sees such a case).  Note that
  7769. if there are any tokens in either the von or last names, then the last
  7770. name has at least one, even if it starts with a lower-case letter.
  7771. @<Procedures and functions for name-string processing@>=
  7772. procedure von_name_ends_and_last_name_starts_stuff;
  7773. label exit;
  7774. begin                           {there may or may not be a von name}
  7775. von_end := last_end - 1;
  7776. while (von_end > von_start) do
  7777.     begin
  7778.     name_bf_ptr := name_tok[von_end-1];
  7779.     name_bf_xptr := name_tok[von_end];
  7780.     if (von_token_found) then
  7781.         return;
  7782.     decr(von_end);
  7783.     end;
  7784. exit:
  7785. This module uses the information in |pop_lit1| to format the name.
  7786. Everything at |sp_brace_level = 0| is copied verbatim to the formatted
  7787. string; the rest is described in the succeeding modules.
  7788. @<Figure out the formatted name@>=
  7789. begin
  7790. ex_buf_ptr := 0;
  7791. sp_brace_level := 0;
  7792. sp_ptr := str_start[pop_lit1];
  7793. sp_end := str_start[pop_lit1+1];
  7794. while (sp_ptr < sp_end) do
  7795.     if (str_pool[sp_ptr] = left_brace) then
  7796.         begin
  7797.         incr(sp_brace_level);
  7798.         incr(sp_ptr);
  7799.         @<Format this part of the name@>;
  7800.         end
  7801.     else if (str_pool[sp_ptr] = right_brace) then
  7802.         begin
  7803.         braces_unbalanced_complaint (pop_lit1);
  7804.         incr(sp_ptr);
  7805.         end
  7806.     else
  7807.         begin
  7808.         append_ex_buf_char_and_check (str_pool[sp_ptr]);
  7809.         incr(sp_ptr);
  7810.         end;
  7811. if (sp_brace_level > 0) then
  7812.     braces_unbalanced_complaint (pop_lit1);
  7813. ex_buf_length := ex_buf_ptr;
  7814. When we arrive here we're at |sp_brace_level = 1|, just past the
  7815. |left_brace|.  Letters at this |sp_brace_level| other than those
  7816. denoting the parts of the name (i.e., the first letters of `first,'
  7817. `last,' `von,' and `jr,' ignoring case) are illegal.  We do two passes
  7818. over this group; the first determines whether we're to output
  7819. anything, and, if we are, the second actually outputs it.
  7820. @<Format this part of the name@>=
  7821. begin
  7822. sp_xptr1 := sp_ptr;
  7823. alpha_found := false;
  7824. double_letter := false;
  7825. end_of_group := false;
  7826. to_be_written := true;
  7827. while ((not end_of_group) and (sp_ptr < sp_end)) do
  7828.     if (lex_class[str_pool[sp_ptr]] = alpha) then
  7829.         begin
  7830.         incr(sp_ptr);
  7831.         @<Figure out what this letter means@>;
  7832.         end
  7833.     else if (str_pool[sp_ptr] = right_brace) then
  7834.         begin
  7835.         decr(sp_brace_level);
  7836.         incr(sp_ptr);
  7837.         end_of_group := true;
  7838.         end
  7839.     else if (str_pool[sp_ptr] = left_brace) then
  7840.         begin
  7841.         incr(sp_brace_level);
  7842.         incr(sp_ptr);
  7843.         skip_stuff_at_sp_brace_level_greater_than_one;
  7844.         end
  7845.     else
  7846.         incr(sp_ptr);
  7847. if ((end_of_group) and (to_be_written)) then    {do the second pass}
  7848.     @<Finally format this part of the name@>;
  7849. When we come here |sp_ptr| is just past the |left_brace|, and when we
  7850. leave it's either at |sp_end| or just past the matching |right_brace|.
  7851. @<Procedures and functions for name-string processing@>=
  7852. procedure skip_stuff_at_sp_brace_level_greater_than_one;
  7853. begin
  7854. while ((sp_brace_level > 1) and (sp_ptr < sp_end)) do
  7855.     begin
  7856.     if (str_pool[sp_ptr] = right_brace) then
  7857.         decr(sp_brace_level)
  7858.     else if (str_pool[sp_ptr] = left_brace) then
  7859.         incr(sp_brace_level);
  7860.     incr(sp_ptr);
  7861.     end;
  7862. We won't output anything for this part of the name if this is a second
  7863. occurrence of an |sp_brace_level = 1| letter, if it's an illegal
  7864. letter, or if there are no tokens corresponding to this part.  We also
  7865. determine if we're we to output complete tokens (indicated by a double
  7866. letter).
  7867. @<Figure out what this letter means@>=
  7868. begin
  7869. if (alpha_found) then
  7870.     begin
  7871.     brace_lvl_one_letters_complaint;
  7872.     to_be_written := false;
  7873.     end
  7874.   else
  7875.     begin
  7876.     case (str_pool[sp_ptr-1]) of
  7877.         "f","F" : @<Figure out what tokens we'll output for the `first' name@>;
  7878.         "v","V" : @<Figure out what tokens we'll output for the `von' name@>;
  7879.         "l","L" : @<Figure out what tokens we'll output for the `last' name@>;
  7880.         "j","J" : @<Figure out what tokens we'll output for the `jr' name@>;
  7881.         othercases
  7882.                 begin
  7883.                 brace_lvl_one_letters_complaint;
  7884.                 to_be_written := false;
  7885.                 end
  7886.     endcases;
  7887.     if (double_letter) then
  7888.         incr(sp_ptr);
  7889.     end;
  7890. alpha_found := true;
  7891. At most one of the important letters, perhaps doubled, may appear at
  7892. |sp_brace_level = 1|.
  7893. @<Procedures and functions for name-string processing@>=
  7894. procedure brace_lvl_one_letters_complaint;
  7895. begin
  7896. print ('The format string "');
  7897. print_pool_str (pop_lit1);
  7898. bst_ex_warn ('" has an illegal brace-level-1 letter');
  7899. Here we set pointers into |name_tok| and note whether we'll be dealing
  7900. with a full first-name tokens (|double_letter = true|) or
  7901. abbreviations (|double_letter = false|).
  7902. @<Figure out what tokens we'll output for the `first' name@>=
  7903. begin
  7904. cur_token := first_start;
  7905. last_token := first_end;
  7906. if (cur_token = last_token) then
  7907.     to_be_written := false;
  7908. if ((str_pool[sp_ptr] = "f") or (str_pool[sp_ptr] = "F")) then
  7909.     double_letter := true;
  7910. The same as above but for von-name tokens.
  7911. @<Figure out what tokens we'll output for the `von' name@>=
  7912. begin
  7913. cur_token := von_start;
  7914. last_token := von_end;
  7915. if (cur_token = last_token) then
  7916.     to_be_written := false;
  7917. if ((str_pool[sp_ptr] = "v") or (str_pool[sp_ptr] = "V")) then
  7918.     double_letter := true;
  7919. The same as above but for last-name tokens.
  7920. @<Figure out what tokens we'll output for the `last' name@>=
  7921. begin
  7922. cur_token := von_end;
  7923. last_token := last_end;
  7924. if (cur_token = last_token) then
  7925.     to_be_written := false;
  7926. if ((str_pool[sp_ptr] = "l") or (str_pool[sp_ptr] = "L")) then
  7927.     double_letter := true;
  7928. The same as above but for jr-name tokens.
  7929. @<Figure out what tokens we'll output for the `jr' name@>=
  7930. begin
  7931. cur_token := last_end;
  7932. last_token := jr_end;
  7933. if (cur_token = last_token) then
  7934.     to_be_written := false;
  7935. if ((str_pool[sp_ptr] = "j") or (str_pool[sp_ptr] = "J")) then
  7936.     double_letter := true;
  7937. This is the second pass over this part of the name; here we actually
  7938. write stuff out to |ex_buf|.
  7939. @<Finally format this part of the name@>=
  7940. begin
  7941. ex_buf_xptr := ex_buf_ptr;
  7942. sp_ptr := sp_xptr1;
  7943. sp_brace_level := 1;
  7944. while (sp_brace_level > 0) do
  7945.     if ((lex_class[str_pool[sp_ptr]] = alpha) and (sp_brace_level = 1)) then
  7946.         begin
  7947.         incr(sp_ptr);
  7948.         @<Figure out how to output the name tokens, and do it@>;
  7949.         end
  7950.     else if (str_pool[sp_ptr] = right_brace) then
  7951.         begin
  7952.         decr(sp_brace_level);
  7953.         incr(sp_ptr);
  7954.         if (sp_brace_level > 0) then
  7955.             append_ex_buf_char_and_check (right_brace);
  7956.         end
  7957.     else if (str_pool[sp_ptr] = left_brace) then
  7958.         begin
  7959.         incr(sp_brace_level);
  7960.         incr(sp_ptr);
  7961.         append_ex_buf_char_and_check (left_brace);
  7962.         end
  7963.     else
  7964.         begin
  7965.         append_ex_buf_char_and_check (str_pool[sp_ptr]);
  7966.         incr(sp_ptr);
  7967.         end;
  7968. if (ex_buf_ptr > 0) then
  7969.   if (ex_buf[ex_buf_ptr-1] = tie) then
  7970.     @<Handle a discretionary |tie|@>;
  7971. When we come here, |sp_ptr| is just past the letter indicating the
  7972. part of the name for which we're about to output tokens.  When we
  7973. leave, it's at the first character of the rest of the group.
  7974. @<Figure out how to output the name tokens, and do it@>=
  7975. begin
  7976. if (double_letter) then
  7977.     incr(sp_ptr);
  7978. use_default := true;
  7979. sp_xptr2 := sp_ptr;
  7980. if (str_pool[sp_ptr] = left_brace) then         {find the inter-token string}
  7981.     begin
  7982.     use_default := false;
  7983.     incr(sp_brace_level);
  7984.     incr(sp_ptr);
  7985.     sp_xptr1 := sp_ptr;
  7986.     skip_stuff_at_sp_brace_level_greater_than_one;
  7987.     sp_xptr2 := sp_ptr - 1;
  7988.     end;
  7989. @<Finally output the name tokens@>;
  7990. if (not use_default) then
  7991.     sp_ptr := sp_xptr2 + 1;
  7992. Here, for each token in this part, we output either a full or an
  7993. abbreviated token and the inter-token string for all but the last
  7994. token of this part.
  7995. @<Finally output the name tokens@>=
  7996. while (cur_token < last_token) do
  7997.     begin
  7998.     if (double_letter) then
  7999.         @<Finally output a full token@>
  8000.       else
  8001.         @<Finally output an abbreviated token@>;
  8002.     incr(cur_token);
  8003.     if (cur_token < last_token) then
  8004.         @<Finally output the inter-token string@>;
  8005.     end
  8006. @:BibTeX capacity exceeded}{\quad buffer size@>
  8007. Here we output all the characters in the token, verbatim.
  8008. @<Finally output a full token@>=
  8009. begin
  8010. name_bf_ptr := name_tok[cur_token];
  8011. name_bf_xptr := name_tok[cur_token+1];
  8012. if (ex_buf_length+(name_bf_xptr-name_bf_ptr) > buf_size) then
  8013.     buffer_overflow;
  8014. while (name_bf_ptr < name_bf_xptr) do
  8015.     begin
  8016.     append_ex_buf_char (name_buf[name_bf_ptr]);
  8017.     incr(name_bf_ptr);
  8018.     end;
  8019. @^special character@>
  8020. Here we output the first alphabetic or special character of the token;
  8021. brace level is irrelevant for an alphabetic (but not a special)
  8022. character.
  8023. @<Finally output an abbreviated token@>=
  8024. begin
  8025. name_bf_ptr := name_tok[cur_token];
  8026. name_bf_xptr := name_tok[cur_token+1];
  8027. while (name_bf_ptr < name_bf_xptr) do
  8028.     begin
  8029.     if (lex_class[name_buf[name_bf_ptr]] = alpha) then
  8030.         begin
  8031.         append_ex_buf_char_and_check (name_buf[name_bf_ptr]);
  8032.         goto loop_exit;
  8033.         end
  8034.     else if ((name_buf[name_bf_ptr] = left_brace) and
  8035.                                 (name_bf_ptr + 1 < name_bf_xptr)) then
  8036.       if (name_buf[name_bf_ptr+1] = backslash) then
  8037.         @<Finally output a special character and exit loop@>;
  8038.     incr(name_bf_ptr);
  8039.     end;
  8040. loop_exit:
  8041. @^special character@>
  8042. @^user abuse@>
  8043. @:BibTeX capacity exceeded}{\quad buffer size@>
  8044. We output a special character here even if the user has been silly
  8045. enough to make it nonalphabetic (and even if the user has been sillier
  8046. still by not having a matching |right_brace|).
  8047. @<Finally output a special character and exit loop@>=
  8048. begin
  8049. if (ex_buf_ptr + 2 > buf_size) then
  8050.     buffer_overflow;
  8051. append_ex_buf_char (left_brace);
  8052. append_ex_buf_char (backslash);
  8053. name_bf_ptr := name_bf_ptr + 2;
  8054. nm_brace_level := 1;
  8055. while ((name_bf_ptr < name_bf_xptr) and (nm_brace_level > 0)) do
  8056.     begin
  8057.     if (name_buf[name_bf_ptr] = right_brace) then
  8058.         decr(nm_brace_level)
  8059.     else if (name_buf[name_bf_ptr] = left_brace) then
  8060.         incr(nm_brace_level);
  8061.     append_ex_buf_char_and_check (name_buf[name_bf_ptr]);
  8062.     incr(name_bf_ptr);
  8063.     end;
  8064. goto loop_exit;
  8065. @:BibTeX capacity exceeded}{\quad buffer size@>
  8066. Here we output either the \.{.bst} given string if it exists, or else
  8067. the \.{.bib} |sep_char| if it exists, or else the default string.  A
  8068. |tie| is the default space character between the last two tokens of
  8069. the name part, and between the first two tokens if the first token is
  8070. short enough; otherwise, a |space| is the default.
  8071. @d long_token = 3       {a token this length or longer is ``long''}
  8072. @<Finally output the inter-token string@>=
  8073. begin
  8074. if (use_default) then
  8075.     begin
  8076.     if (not double_letter) then
  8077.         append_ex_buf_char_and_check (period);
  8078.     if (lex_class[name_sep_char[cur_token]] = sep_char) then
  8079.         append_ex_buf_char_and_check (name_sep_char[cur_token])
  8080.     else if ((cur_token = last_token-1) or
  8081.                         (not enough_text_chars (long_token))) then
  8082.         append_ex_buf_char_and_check (tie)
  8083.     else
  8084.         append_ex_buf_char_and_check (space);
  8085.     end
  8086.   else
  8087.     begin
  8088.     if (ex_buf_length+(sp_xptr2-sp_xptr1) > buf_size) then
  8089.         buffer_overflow;
  8090.     sp_ptr := sp_xptr1;
  8091.     while (sp_ptr < sp_xptr2) do
  8092.         begin
  8093.         append_ex_buf_char (str_pool[sp_ptr]);
  8094.         incr(sp_ptr);
  8095.         end
  8096.     end;
  8097. @^special character@>
  8098. This function looks at the string in |ex_buf|, starting at
  8099. |ex_buf_xptr| and ending just before |ex_buf_ptr|, and it returns
  8100. |true| if there are |enough_chars|, where a special character (even if
  8101. it's missing its matching |right_brace|) counts as a single charcter.
  8102. This procedure is called only for strings that don't have too many
  8103. |right_brace|s.
  8104. @<Procedures and functions for name-string processing@>=
  8105. function enough_text_chars (@!enough_chars : buf_pointer) : boolean;
  8106. begin
  8107. num_text_chars := 0;
  8108. ex_buf_yptr := ex_buf_xptr;
  8109. while ((ex_buf_yptr < ex_buf_ptr) and (num_text_chars < enough_chars)) do
  8110.     begin
  8111.     incr(ex_buf_yptr);
  8112.     if (ex_buf[ex_buf_yptr-1] = left_brace) then
  8113.         begin
  8114.         incr(brace_level);
  8115.         if ((brace_level = 1) and (ex_buf_yptr < ex_buf_ptr)) then
  8116.           if (ex_buf[ex_buf_yptr] = backslash) then
  8117.             begin
  8118.             incr(ex_buf_yptr);                  {skip over the |backslash|}
  8119.             while ((ex_buf_yptr < ex_buf_ptr) and (brace_level > 0)) do
  8120.                 begin
  8121.                 if (ex_buf[ex_buf_yptr] = right_brace) then
  8122.                     decr(brace_level)
  8123.                 else if (ex_buf[ex_buf_yptr] = left_brace) then
  8124.                     incr(brace_level);
  8125.                 incr(ex_buf_yptr);
  8126.                 end;
  8127.             end;
  8128.         end
  8129.     else if (ex_buf[ex_buf_yptr-1] = right_brace) then
  8130.         decr(brace_level);
  8131.     incr(num_text_chars);
  8132.     end;
  8133. if (num_text_chars < enough_chars) then
  8134.     enough_text_chars := false
  8135.   else
  8136.     enough_text_chars := true;
  8137. If the last character output for this name part is a |tie| but the
  8138. previous character it isn't, we're dealing with a discretionary |tie|;
  8139. thus we replace it by a |space| if there are enough characters in the
  8140. rest of the name part.
  8141. @d long_name = 3                {a name this length or longer is ``long''}
  8142. @<Handle a discretionary |tie|@>=
  8143. begin
  8144. decr(ex_buf_ptr);                       {remove the previous |tie|}
  8145. if (ex_buf[ex_buf_ptr-1] = tie) then    {it's not a discretionary |tie|}
  8146.     do_nothing
  8147. else if (not enough_text_chars (long_name)) then {this is a short name part}
  8148.     incr(ex_buf_ptr)                    {so restore the |tie|}
  8149. else                                    {replace it by a |space|}
  8150.     append_ex_buf_char (space);
  8151. This is a procedure so that |x_format_name| is smaller.
  8152. @<Procedures and functions for name-string processing@>=
  8153. procedure figure_out_the_formatted_name;
  8154. label loop_exit;
  8155. begin
  8156. @<Figure out the formatted name@>;
  8157. The |built_in| function {\.{if\$}} pops the top three literals (they
  8158. are two function literals and an integer literal, in that order); if
  8159. the integer is greater than 0, it executes the second literal, else it
  8160. executes the first.  If any of the types is incorrect, it complains
  8161. but does nothing else.
  8162. @<|execute_fn|({\.{if\$}})@>=
  8163. begin
  8164. pop_lit_stk (pop_lit1,pop_typ1);
  8165. pop_lit_stk (pop_lit2,pop_typ2);
  8166. pop_lit_stk (pop_lit3,pop_typ3);
  8167. if (pop_typ1 <> stk_fn) then
  8168.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_fn)
  8169. else if (pop_typ2 <> stk_fn) then
  8170.     print_wrong_stk_lit (pop_lit2,pop_typ2,stk_fn)
  8171. else if (pop_typ3 <> stk_int) then
  8172.     print_wrong_stk_lit (pop_lit3,pop_typ3,stk_int)
  8173.     if (pop_lit3 > 0) then
  8174.         execute_fn (pop_lit2)
  8175.       else
  8176.         execute_fn (pop_lit1);
  8177. The |built_in| function {\.{int.to.chr\$}} pops the top (integer)
  8178. literal, interpreted as the |ASCII_code| of a single character,
  8179. converts it to the corresponding single-character string, and pushes
  8180. this string.  If the literal isn't an appropriate integer, it
  8181. complains and pushes the null string.
  8182. @<|execute_fn|({\.{int.to.chr\$}})@>=
  8183. procedure x_int_to_chr;
  8184. begin
  8185. pop_lit_stk (pop_lit1,pop_typ1);
  8186. if (pop_typ1 <> stk_int) then
  8187.     begin
  8188.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_int);
  8189.     push_lit_stk (s_null, stk_str);
  8190.     end
  8191. else if ((pop_lit1 < 0) or (pop_lit1 > 127)) then
  8192.     begin
  8193.     bst_ex_warn (pop_lit1:0,' isn''t valid ASCII');
  8194.     push_lit_stk (s_null, stk_str);
  8195.     end
  8196.     begin
  8197.     str_room(1);
  8198.     append_char (pop_lit1);
  8199.     push_lit_stk (make_string, stk_str);
  8200.     end;
  8201. The |built_in| function {\.{int.to.str\$}} pops the top (integer)
  8202. literal, converts it to its (unique) string equivalent, and pushes
  8203. this string.  If the literal isn't an integer, it complains and pushes
  8204. the null string.
  8205. @<|execute_fn|({\.{int.to.str\$}})@>=
  8206. procedure x_int_to_str;
  8207. begin
  8208. pop_lit_stk (pop_lit1,pop_typ1);
  8209. if (pop_typ1 <> stk_int) then
  8210.     begin
  8211.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_int);
  8212.     push_lit_stk (s_null, stk_str);
  8213.     end
  8214.     begin
  8215.     int_to_ASCII (pop_lit1, ex_buf, 0, ex_buf_length);@/
  8216.     add_pool_buf_and_push;              {push this string onto the stack}
  8217.     end;
  8218. The |built_in| function {\.{missing\$}} pops the top literal and
  8219. pushes the integer 1 if it's a missing field, 0 otherwise.  If the
  8220. literal isn't a missing field or a string, it complains and pushes 0.
  8221. Unlike \.{empty\$}, this function should be called only when
  8222. |mess_with_entries| is true.
  8223. @<|execute_fn|({\.{missing\$}})@>=
  8224. procedure x_missing;
  8225. begin
  8226. pop_lit_stk (pop_lit1,pop_typ1);
  8227. if (not mess_with_entries) then
  8228.     bst_cant_mess_with_entries_print
  8229. else if ((pop_typ1 <> stk_str) and (pop_typ1 <> stk_field_missing)) then
  8230.     begin
  8231.     if (pop_typ1 <> stk_empty) then
  8232.         begin
  8233.         print_stk_lit (pop_lit1,pop_typ1);
  8234.         bst_ex_warn (', not a string or missing field,');
  8235.         end;
  8236.     push_lit_stk (0, stk_int);
  8237.     end
  8238.     if (pop_typ1 = stk_field_missing) then
  8239.         push_lit_stk (1, stk_int)
  8240.       else
  8241.         push_lit_stk (0, stk_int);
  8242. The |built_in| function {\.{newline\$}} writes whatever has
  8243. accumulated in the output buffer |out_buf| onto the \.{.bbl} file.
  8244. @<|execute_fn|({\.{newline\$}})@>=
  8245. begin
  8246. output_bbl_line;
  8247. The |built_in| function {\.{num.names\$}} pops the top (string)
  8248. literal; it pushes the number of names the string represents---one
  8249. plus the number of occurrences of the substring ``and'' (ignoring case
  8250. differences) surrounded by nonnull |white_space| at the top brace
  8251. level.  If the literal isn't a string, it complains and pushes the
  8252. value 0.
  8253. @<|execute_fn|({\.{num.names\$}})@>=
  8254. procedure x_num_names;
  8255. begin
  8256. pop_lit_stk (pop_lit1,pop_typ1);
  8257. if (pop_typ1 <> stk_str) then
  8258.     begin
  8259.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str);
  8260.     push_lit_stk (0, stk_int);
  8261.     end
  8262.     begin
  8263.     ex_buf_length := 0;
  8264.     add_buf_pool (pop_lit1);
  8265.     @<Determine the number of names@>;
  8266.     push_lit_stk (num_names, stk_int);
  8267.     end;
  8268. This module, while scanning the list of names, counts the occurrences
  8269. of ``and'' (ignoring case differences) surrounded by nonnull
  8270. |white_space|, and adds 1.
  8271. @<Determine the number of names@>=
  8272. begin
  8273. ex_buf_ptr := 0;
  8274. num_names := 0;
  8275. while (ex_buf_ptr < ex_buf_length) do
  8276.     begin
  8277.     name_scan_for_and (pop_lit1);
  8278.     incr(num_names);
  8279.     end;
  8280. The |built_in| function {\.{pop\$}} pops the top of the stack but
  8281. doesn't print it.
  8282. @<|execute_fn|({\.{pop\$}})@>=
  8283. begin
  8284. pop_lit_stk (pop_lit1,pop_typ1);
  8285. The |built_in| function {\.{preamble\$}} pushes onto the stack the
  8286. concatenation of all the \.{preamble} strings read from the database
  8287. files.
  8288. @<|execute_fn|({\.{preamble\$}})@>=
  8289. procedure x_preamble;
  8290. begin
  8291. ex_buf_length := 0;
  8292. preamble_ptr := 0;
  8293. while (preamble_ptr < num_preamble_strings) do
  8294.     begin
  8295.     add_buf_pool (s_preamble[preamble_ptr]);
  8296.     incr(preamble_ptr);
  8297.     end;
  8298. add_pool_buf_and_push;          {push the concatenation string onto the stack}
  8299. @^special character@>
  8300. The |built_in| function {\.{purify\$}} pops the top (string) literal,
  8301. removes nonalphanumeric characters except for |white_space| and
  8302. |sep_char| characters (these get converted to a |space|) and removes
  8303. certain alphabetic characters contained in the control sequences
  8304. associated with a special character, and pushes the resulting string.
  8305. If the literal isn't a string, it complains and pushes the null
  8306. string.
  8307. @<|execute_fn|({\.{purify\$}})@>=
  8308. procedure x_purify;
  8309. begin
  8310. pop_lit_stk (pop_lit1,pop_typ1);
  8311. if (pop_typ1 <> stk_str) then
  8312.     begin
  8313.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str);
  8314.     push_lit_stk (s_null, stk_str);
  8315.     end
  8316.     begin
  8317.     ex_buf_length := 0;
  8318.     add_buf_pool (pop_lit1);
  8319.     @<Perform the purification@>;
  8320.     add_pool_buf_and_push;              {push this string onto the stack}
  8321.     end;
  8322. @^special character@>
  8323. The resulting string has nonalphanumeric characters removed, and each
  8324. |white_space| or |sep_char| character converted to a |space|.  The next
  8325. module handles special characters.  This code doesn't complain if the
  8326. string isn't brace balanced.
  8327. @<Perform the purification@>=
  8328. begin
  8329. brace_level := 0;       {this is the top level}
  8330. ex_buf_xptr := 0;       {this pointer is for the purified string}
  8331. ex_buf_ptr := 0;        {and this one is for the original string}
  8332. while (ex_buf_ptr < ex_buf_length) do
  8333.     begin
  8334.     case (lex_class[ex_buf[ex_buf_ptr]]) of
  8335.         white_space,
  8336.         sep_char :
  8337.             begin
  8338.             ex_buf[ex_buf_xptr] := space;
  8339.             incr(ex_buf_xptr);
  8340.             end;
  8341.         alpha,
  8342.         numeric :
  8343.             begin
  8344.             ex_buf[ex_buf_xptr] := ex_buf[ex_buf_ptr];
  8345.             incr(ex_buf_xptr);
  8346.             end;
  8347.         othercases
  8348.             if (ex_buf[ex_buf_ptr] = left_brace) then
  8349.                 begin
  8350.                 incr(brace_level);
  8351.                 if ((brace_level = 1) and
  8352.                                 (ex_buf_ptr + 1 < ex_buf_length)) then
  8353.                   if (ex_buf[ex_buf_ptr+1] = backslash) then
  8354.                     @<Purify a special character@>;
  8355.                 end
  8356.             else if (ex_buf[ex_buf_ptr] = right_brace) then
  8357.                 if (brace_level > 0) then
  8358.                     decr(brace_level)
  8359.     endcases;
  8360.     incr(ex_buf_ptr);
  8361.     end;
  8362. ex_buf_length := ex_buf_xptr;
  8363. @^special character@>
  8364. Special characters (even without a matching |right_brace|) are
  8365. purified by removing the control sequences (but restoring the correct
  8366. thing for `\.{\\i}' and `\.{\\j}' as well as the eleven alphabetic
  8367. foreign characters in Table~3.2 of the \LaTeX\ manual) and removing
  8368. all nonalphanumeric characters (including |white_space| and
  8369. |sep_char|s).
  8370. @<Purify a special character@>=
  8371. begin
  8372. incr(ex_buf_ptr);                       {skip over the |left_brace|}
  8373. while ((ex_buf_ptr < ex_buf_length) and (brace_level > 0)) do
  8374.     begin
  8375.     incr(ex_buf_ptr);                   {skip over the |backslash|}
  8376.     ex_buf_yptr := ex_buf_ptr;  {mark the beginning of the control sequence}
  8377.     while ((ex_buf_ptr < ex_buf_length) and
  8378.                 (lex_class[ex_buf[ex_buf_ptr]] = alpha)) do@/
  8379.         incr(ex_buf_ptr);               {this scans the control sequence}
  8380.     control_seq_loc := str_lookup(ex_buf,ex_buf_yptr,ex_buf_ptr-ex_buf_yptr,
  8381.                                                 control_seq_ilk,dont_insert);
  8382.     if (hash_found) then
  8383.         @<Purify this accented or foreign character@>;
  8384.     while ((ex_buf_ptr < ex_buf_length) and (brace_level > 0) and
  8385.                                         (ex_buf[ex_buf_ptr] <> backslash)) do
  8386.         begin                   {this scans to the next control sequence}
  8387.         case (lex_class[ex_buf[ex_buf_ptr]]) of
  8388.             alpha,
  8389.             numeric :
  8390.                 begin
  8391.                 ex_buf[ex_buf_xptr] := ex_buf[ex_buf_ptr];
  8392.                 incr(ex_buf_xptr);
  8393.                 end;
  8394.             othercases
  8395.                 if (ex_buf[ex_buf_ptr] = right_brace) then
  8396.                     decr(brace_level)
  8397.                 else if (ex_buf[ex_buf_ptr] = left_brace) then
  8398.                     incr(brace_level)
  8399.         endcases;
  8400.         incr(ex_buf_ptr);
  8401.         end;
  8402.     end;
  8403. decr(ex_buf_ptr);               {unskip the |right_brace| (or last character)}
  8404. We consider the purified character to be either the first alphabetic
  8405. character of its control sequence, or perhaps both alphabetic
  8406. characters.
  8407. @<Purify this accented or foreign character@>=
  8408. begin
  8409. ex_buf[ex_buf_xptr] := ex_buf[ex_buf_yptr]; {the first alphabetic character}
  8410. incr(ex_buf_xptr);
  8411. case (ilk_info[control_seq_loc]) of
  8412.     n_oe,
  8413.     n_oe_upper,
  8414.     n_ae,
  8415.     n_ae_upper,
  8416.     n_ss :
  8417.         begin                                   {and the second}
  8418.         ex_buf[ex_buf_xptr] := ex_buf[ex_buf_yptr+1];
  8419.         incr(ex_buf_xptr);
  8420.         end;
  8421.     othercases
  8422.         do_nothing
  8423. endcases;
  8424. The |built_in| function {\.{quote\$}} pushes the string consisting of
  8425. the |double_quote| character.
  8426. @<|execute_fn|({\.{quote\$}})@>=
  8427. procedure x_quote;
  8428. begin
  8429. str_room(1);
  8430. append_char (double_quote);
  8431. push_lit_stk (make_string, stk_str);
  8432. The |built_in| function {\.{skip\$}} is a no-op.
  8433. @<|execute_fn|({\.{skip\$}})@>=
  8434. begin
  8435. do_nothing;
  8436. The |built_in| function {\.{stack\$}} pops and prints the whole stack;
  8437. it's meant to be used for style designers while debugging.
  8438. @<|execute_fn|({\.{stack\$}})@>=
  8439. begin
  8440. pop_whole_stack;
  8441. @^push the literal stack@>
  8442. The |built_in| function {\.{substring\$}} pops the top three literals
  8443. (they are the two integers literals |pop_lit1| and |pop_lit2| and a
  8444. string literal, in that order).  It pushes the substring of the (at
  8445. most) |pop_lit1| consecutive characters starting at the |pop_lit2|th
  8446. character (assuming 1-based indexing) if |pop_lit2| is positive, and
  8447. ending at the |-pop_lit2|th character from the end if |pop_lit2| is
  8448. negative (where the first character from the end is the last
  8449. character).  If any of the types is incorrect, it complain and pushes
  8450. the null string.
  8451. @<|execute_fn|({\.{substring\$}})@>=
  8452. procedure x_substring;
  8453. label exit;
  8454. begin
  8455. pop_lit_stk (pop_lit1,pop_typ1);
  8456. pop_lit_stk (pop_lit2,pop_typ2);
  8457. pop_lit_stk (pop_lit3,pop_typ3);
  8458. if (pop_typ1 <> stk_int) then
  8459.     begin
  8460.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_int);
  8461.     push_lit_stk (s_null, stk_str);
  8462.     end
  8463. else if (pop_typ2 <> stk_int) then
  8464.     begin
  8465.     print_wrong_stk_lit (pop_lit2,pop_typ2,stk_int);
  8466.     push_lit_stk (s_null, stk_str);
  8467.     end
  8468. else if (pop_typ3 <> stk_str) then
  8469.     begin
  8470.     print_wrong_stk_lit (pop_lit3,pop_typ3,stk_str);
  8471.     push_lit_stk (s_null, stk_str);
  8472.     end
  8473.     begin
  8474.     sp_length := length(pop_lit3);
  8475.     if (pop_lit1 >= sp_length) then
  8476.       if ((pop_lit2 = 1) or (pop_lit2 = -1)) then
  8477.         begin
  8478.         repush_string;
  8479.         return;
  8480.         end;
  8481.     if ((pop_lit1 <= 0) or (pop_lit2 = 0) or (pop_lit2 > sp_length) or
  8482.                                         (pop_lit2 < -sp_length)) then
  8483.         begin
  8484.         push_lit_stk (s_null, stk_str);
  8485.         return;
  8486.         end
  8487.       else
  8488.         @<Form the appropriate substring@>;
  8489.     end;
  8490. exit:
  8491. @^push the literal stack@>
  8492. This module finds the substring as described in the last section,
  8493. and slides it into place in the string pool, if necessary.
  8494. @<Form the appropriate substring@>=
  8495. begin
  8496. if (pop_lit2 > 0) then
  8497.     begin
  8498.     if (pop_lit1 > sp_length - (pop_lit2-1)) then
  8499.         pop_lit1 := sp_length - (pop_lit2-1);
  8500.     sp_ptr := str_start[pop_lit3] + (pop_lit2-1);
  8501.     sp_end := sp_ptr + pop_lit1;
  8502.     if (pop_lit2 = 1) then
  8503.       if (pop_lit3 >= cmd_str_ptr) then {no shifting---merely change pointers}
  8504.         begin
  8505.         str_start[pop_lit3+1] := sp_end;
  8506.         unflush_string;
  8507.         incr(lit_stk_ptr);
  8508.         return;
  8509.         end;
  8510.     end
  8511. else                                    {|-ex_buf_length <= pop_lit2 < 0|}
  8512.     begin
  8513.     pop_lit2 := -pop_lit2;
  8514.     if (pop_lit1 > sp_length - (pop_lit2-1)) then
  8515.         pop_lit1 := sp_length - (pop_lit2-1);
  8516.     sp_end := str_start[pop_lit3+1] - (pop_lit2-1);
  8517.     sp_ptr := sp_end - pop_lit1;
  8518.     end;
  8519. while (sp_ptr < sp_end) do                      {shift the substring}
  8520.     begin
  8521.     append_char (str_pool[sp_ptr]);
  8522.     incr(sp_ptr);
  8523.     end;
  8524. push_lit_stk (make_string, stk_str);            {and push it onto the stack}
  8525. The |built_in| function {\.{swap\$}} pops the top two literals from
  8526. the stack and pushes them back swapped.
  8527. @<|execute_fn|({\.{swap\$}})@>=
  8528. procedure x_swap;
  8529. begin
  8530. pop_lit_stk (pop_lit1,pop_typ1);
  8531. pop_lit_stk (pop_lit2,pop_typ2);
  8532. if ((pop_typ1 <> stk_str) or (pop_lit1 < cmd_str_ptr)) then
  8533.     begin
  8534.     push_lit_stk (pop_lit1, pop_typ1);
  8535.     if ((pop_typ2 = stk_str) and (pop_lit2 >= cmd_str_ptr)) then
  8536.         unflush_string;
  8537.     push_lit_stk (pop_lit2, pop_typ2);
  8538.     end
  8539. else if ((pop_typ2 <> stk_str) or (pop_lit2 < cmd_str_ptr)) then
  8540.     begin
  8541.     unflush_string;                     {this is |pop_lit1|}
  8542.     push_lit_stk (pop_lit1, stk_str);
  8543.     push_lit_stk (pop_lit2, pop_typ2);
  8544.     end
  8545. else                                    {bummer, both are recent strings}
  8546.     @<Swap the two strings (they're at the end of |str_pool|)@>;
  8547. We have to swap both (a)~the strings at the end of the string pool,
  8548. and (b)~their pointers on the literal stack.
  8549. @<Swap the two strings (they're at the end of |str_pool|)@>=
  8550. begin
  8551. ex_buf_length := 0;
  8552. add_buf_pool (pop_lit2);                {save the second string}
  8553. sp_ptr := str_start[pop_lit1];
  8554. sp_end := str_start[pop_lit1+1];
  8555. while (sp_ptr < sp_end) do              {slide the first string down}
  8556.     begin
  8557.     append_char (str_pool[sp_ptr]);
  8558.     incr(sp_ptr);
  8559.     end;
  8560. push_lit_stk (make_string, stk_str);    {and push it onto the stack}
  8561. add_pool_buf_and_push;                  {push second string onto the stack}
  8562. @^special character@>
  8563. The |built_in| function {\.{text.length\$}} pops the top (string)
  8564. literal, and pushes the number of text characters it contains, where
  8565. an accented character (more precisely, a ``special character''$\!$,
  8566. defined earlier) counts as a single text character, even if it's
  8567. missing its matching |right_brace|, and where braces don't count as
  8568. text characters.  If the literal isn't a string, it complains and
  8569. pushes the null string.
  8570. @<|execute_fn|({\.{text.length\$}})@>=
  8571. procedure x_text_length;
  8572. begin
  8573. pop_lit_stk (pop_lit1,pop_typ1);
  8574. if (pop_typ1 <> stk_str) then
  8575.     begin
  8576.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str);
  8577.     push_lit_stk (s_null, stk_str);
  8578.     end
  8579.     begin
  8580.     num_text_chars := 0;
  8581.     @<Count the text characters@>;
  8582.     push_lit_stk (num_text_chars, stk_int);     {and push it onto the stack}
  8583.     end;
  8584. @^special character@>
  8585. Here we determine the number of text characters in the string, where
  8586. an entire special character counts as a single text character (even if
  8587. it's missing its matching |right_brace|), and where braces don't count
  8588. as text characters.
  8589. @<Count the text characters@>=
  8590. begin
  8591. sp_ptr := str_start[pop_lit1];
  8592. sp_end := str_start[pop_lit1+1];
  8593. sp_brace_level := 0;
  8594. while (sp_ptr < sp_end) do
  8595.     begin
  8596.     incr(sp_ptr);
  8597.     if (str_pool[sp_ptr-1] = left_brace) then
  8598.         begin
  8599.         incr(sp_brace_level);
  8600.         if ((sp_brace_level = 1) and (sp_ptr < sp_end)) then
  8601.           if (str_pool[sp_ptr] = backslash) then
  8602.             begin
  8603.             incr(sp_ptr);               {skip over the |backslash|}
  8604.             while ((sp_ptr < sp_end) and (sp_brace_level > 0)) do
  8605.                 begin
  8606.                 if (str_pool[sp_ptr] = right_brace) then
  8607.                     decr(sp_brace_level)
  8608.                 else if (str_pool[sp_ptr] = left_brace) then
  8609.                     incr(sp_brace_level);
  8610.                 incr(sp_ptr);
  8611.                 end;
  8612.             incr(num_text_chars);
  8613.             end;
  8614.         end
  8615.     else if (str_pool[sp_ptr-1] = right_brace) then
  8616.         begin
  8617.         if (sp_brace_level > 0) then
  8618.             decr(sp_brace_level);
  8619.         end
  8620.     else
  8621.         incr(num_text_chars);
  8622.     end;
  8623. @^special character@>
  8624. The |built_in| function {\.{text.prefix\$}} pops the top two literals
  8625. (the integer literal |pop_lit1| and a string literal, in that order).
  8626. It pushes the substring of the (at most) |pop_lit1| consecutive text
  8627. characters starting from the beginning of the string.  This function
  8628. is similar to {\.{substring\$}}, but this one considers an accented
  8629. character (or more precisely, a ``special character''$\!$, even if
  8630. it's missing its matching |right_brace|) to be a single text character
  8631. (rather than however many |ASCII_code| characters it actually
  8632. comprises), and this function doesn't consider braces to be text
  8633. characters; furthermore, this function appends any needed matching
  8634. |right_brace|s.  If any of the types is incorrect, it complains and
  8635. pushes the null string.
  8636. @<|execute_fn|({\.{text.prefix\$}})@>=
  8637. procedure x_text_prefix;
  8638. label exit;
  8639. begin
  8640. pop_lit_stk (pop_lit1,pop_typ1);
  8641. pop_lit_stk (pop_lit2,pop_typ2);
  8642. if (pop_typ1 <> stk_int) then
  8643.     begin
  8644.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_int);
  8645.     push_lit_stk (s_null, stk_str);
  8646.     end
  8647. else if (pop_typ2 <> stk_str) then
  8648.     begin
  8649.     print_wrong_stk_lit (pop_lit2,pop_typ2,stk_str);
  8650.     push_lit_stk (s_null, stk_str);
  8651.     end
  8652. else if (pop_lit1 <= 0) then
  8653.     begin
  8654.     push_lit_stk (s_null, stk_str);
  8655.     return;
  8656.     end
  8657.     @<Form the appropriate prefix@>;
  8658. exit:
  8659. @^push the literal stack@>
  8660. This module finds the prefix as described in the last section, and
  8661. appends any needed matching |right_brace|s.
  8662. @<Form the appropriate prefix@>=
  8663. begin
  8664. sp_ptr := str_start[pop_lit2];
  8665. sp_end := str_start[pop_lit2+1];        {this may change}
  8666. @<Scan the appropriate number of characters@>;
  8667. if (pop_lit2 >= cmd_str_ptr) then       {no shifting---merely change pointers}
  8668.     pool_ptr := sp_end
  8669.     while (sp_ptr < sp_end) do          {shift the substring}
  8670.         begin
  8671.         append_char (str_pool[sp_ptr]);
  8672.         incr(sp_ptr);
  8673.         end;
  8674. while (sp_brace_level > 0) do           {add matching |right_brace|s}
  8675.     begin
  8676.     append_char (right_brace);
  8677.     decr(sp_brace_level);
  8678.     end;
  8679. push_lit_stk (make_string, stk_str);    {and push it onto the stack}
  8680. @^special character@>
  8681. This section scans |pop_lit1| text characters, where an entire special
  8682. character counts as a single text character (even if it's missing its
  8683. matching |right_brace|), and where braces don't count as text
  8684. characters.
  8685. @<Scan the appropriate number of characters@>=
  8686. begin
  8687. num_text_chars := 0;
  8688. sp_brace_level := 0;
  8689. sp_xptr1 := sp_ptr;
  8690. while ((sp_xptr1 < sp_end) and (num_text_chars < pop_lit1)) do
  8691.     begin
  8692.     incr(sp_xptr1);
  8693.     if (str_pool[sp_xptr1-1] = left_brace) then
  8694.         begin
  8695.         incr(sp_brace_level);
  8696.         if ((sp_brace_level = 1) and (sp_xptr1 < sp_end)) then
  8697.           if (str_pool[sp_xptr1] = backslash) then
  8698.             begin
  8699.             incr(sp_xptr1);             {skip over the |backslash|}
  8700.             while ((sp_xptr1 < sp_end) and (sp_brace_level > 0)) do
  8701.                 begin
  8702.                 if (str_pool[sp_xptr1] = right_brace) then
  8703.                     decr(sp_brace_level)
  8704.                 else if (str_pool[sp_xptr1] = left_brace) then
  8705.                     incr(sp_brace_level);
  8706.                 incr(sp_xptr1);
  8707.                 end;
  8708.             incr(num_text_chars);
  8709.             end;
  8710.         end
  8711.     else if (str_pool[sp_xptr1-1] = right_brace) then
  8712.         begin
  8713.         if (sp_brace_level > 0) then
  8714.             decr(sp_brace_level);
  8715.         end
  8716.     else
  8717.         incr(num_text_chars);
  8718.     end;
  8719. sp_end := sp_xptr1;
  8720. The |built_in| function {\.{top\$}} pops and prints the top of the
  8721. stack.
  8722. @<|execute_fn|({\.{top\$}})@>=
  8723. begin
  8724. pop_top_and_print;
  8725. The |built_in| function {\.{type\$}} pushes the appropriate string
  8726. from |type_list| onto the stack (unless either it's |undefined| or
  8727. |empty|, in which case it pushes the null string).
  8728. @<|execute_fn|({\.{type\$}})@>=
  8729. procedure x_type;
  8730. begin
  8731. if (not mess_with_entries) then
  8732.     bst_cant_mess_with_entries_print
  8733.   else
  8734.     if ((type_list[cite_ptr] = undefined) or
  8735.                                 (type_list[cite_ptr] = empty)) then
  8736.         push_lit_stk (s_null, stk_str)
  8737.       else
  8738.         push_lit_stk (hash_text[type_list[cite_ptr]], stk_str);
  8739. The |built_in| function {\.{warning\$}} pops the top (string) literal
  8740. and prints it following a warning message.  This is implemented as a
  8741. special |built_in| function rather than using the {\.{top\$}} function
  8742. so that it can |mark_warning|.
  8743. @<|execute_fn|({\.{warning\$}})@>=
  8744. procedure x_warning;
  8745. begin
  8746. pop_lit_stk (pop_lit1,pop_typ1);
  8747. if (pop_typ1 <> stk_str) then
  8748.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str)
  8749.     begin
  8750.     print ('Warning--');
  8751.     print_lit (pop_lit1,pop_typ1);
  8752.     mark_warning;
  8753.     end;
  8754. The |built_in| function {\.{while\$}} pops the top two (function)
  8755. literals, and keeps executing the second as long as the (integer)
  8756. value left on the stack by executing the first is greater than 0.  If
  8757. either type is incorrect, it complains but does nothing else.
  8758. @<|execute_fn|({\.{while\$}})@>=
  8759. begin
  8760. pop_lit_stk (r_pop_lt1,r_pop_tp1);
  8761. pop_lit_stk (r_pop_lt2,r_pop_tp2);
  8762. if (r_pop_tp1 <> stk_fn) then
  8763.     print_wrong_stk_lit (r_pop_lt1,r_pop_tp1,stk_fn)
  8764. else if (r_pop_tp2 <> stk_fn) then
  8765.     print_wrong_stk_lit (r_pop_lt2,r_pop_tp2,stk_fn)
  8766.     loop
  8767.         begin
  8768.         execute_fn (r_pop_lt2);                 {this is the \.{while\$} test}
  8769.         pop_lit_stk (pop_lit1,pop_typ1);
  8770.         if (pop_typ1 <> stk_int) then
  8771.             begin
  8772.             print_wrong_stk_lit (pop_lit1,pop_typ1,stk_int);
  8773.             goto end_while;
  8774.             end
  8775.         else
  8776.             if (pop_lit1 > 0) then
  8777.                 execute_fn (r_pop_lt1)          {this is the \.{while\$} body}
  8778.               else
  8779.                 goto end_while;
  8780.         end;
  8781. end_while:      {justifies this |mean_while|}
  8782. @^literal literal@>
  8783. @^special character@>
  8784. The |built_in| function {\.{width\$}} pops the top (string) literal
  8785. and pushes the integer that represents its width in units specified by
  8786. the |char_width| array.  This function takes the literal literally;
  8787. that is, it assumes each character in the string is to be printed as
  8788. is, regardless of whether the character has a special meaning to \TeX,
  8789. except that special characters (even without their |right_brace|s) are
  8790. handled specially.  If the literal isn't a string, it complains and
  8791. pushes~0.
  8792. @<|execute_fn|({\.{width\$}})@>=
  8793. procedure x_width;
  8794. begin
  8795. pop_lit_stk (pop_lit1,pop_typ1);
  8796. if (pop_typ1 <> stk_str) then
  8797.     begin
  8798.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str);
  8799.     push_lit_stk (0, stk_int);
  8800.     end
  8801.     begin
  8802.     ex_buf_length := 0;
  8803.     add_buf_pool (pop_lit1);
  8804.     string_width := 0;
  8805.     @<Add up the |char_width|s in this string@>;
  8806.     push_lit_stk (string_width, stk_int);
  8807.     end
  8808. We use the natural width for all but special characters, and we
  8809. complain if the string isn't brace-balanced.
  8810. @<Add up the |char_width|s in this string@>=
  8811. begin
  8812. brace_level := 0;                       {we're at the top level}
  8813. ex_buf_ptr := 0;                        {and the beginning of string}
  8814. while (ex_buf_ptr < ex_buf_length) do
  8815.     begin
  8816.     if (ex_buf[ex_buf_ptr] = left_brace) then
  8817.         begin
  8818.         incr(brace_level);
  8819.         if ((brace_level = 1) and (ex_buf_ptr + 1 < ex_buf_length)) then
  8820.             if (ex_buf[ex_buf_ptr+1] = backslash) then
  8821.                 @<Determine the width of this special character@>
  8822.               else
  8823.                 string_width := string_width + char_width[left_brace]
  8824.           else
  8825.             string_width := string_width + char_width[left_brace];
  8826.         end
  8827.     else if (ex_buf[ex_buf_ptr] = right_brace) then
  8828.         begin
  8829.         decr_brace_level (pop_lit1);
  8830.         string_width := string_width + char_width[right_brace];
  8831.         end
  8832.     else
  8833.         string_width := string_width + char_width[ex_buf[ex_buf_ptr]];
  8834.     incr(ex_buf_ptr);
  8835.     end;
  8836. check_brace_level (pop_lit1);
  8837. @^special character@>
  8838. We use the natural widths of all characters except that some
  8839. characters have no width: braces, control sequences (except for the
  8840. usual 13 accented and foreign characters, whose widths are given in
  8841. the next module), and |white_space| following control sequences (even
  8842. a null control sequence).
  8843. @<Determine the width of this special character@>=
  8844. begin
  8845. incr(ex_buf_ptr);                               {skip over the |left_brace|}
  8846. while ((ex_buf_ptr < ex_buf_length) and (brace_level > 0)) do
  8847.     begin
  8848.     incr(ex_buf_ptr);                   {skip over the |backslash|}
  8849.     ex_buf_xptr := ex_buf_ptr;
  8850.     while ((ex_buf_ptr < ex_buf_length) and
  8851.                 (lex_class[ex_buf[ex_buf_ptr]] = alpha)) do@/
  8852.         incr(ex_buf_ptr);               {this scans the control sequence}
  8853.     if ((ex_buf_ptr < ex_buf_length) and (ex_buf_ptr = ex_buf_xptr)) then
  8854.         incr(ex_buf_ptr)                {this skips a nonalpha control seq}
  8855.       else
  8856.         begin
  8857.         control_seq_loc := str_lookup(ex_buf,ex_buf_xptr,
  8858.                         ex_buf_ptr-ex_buf_xptr,control_seq_ilk,dont_insert);
  8859.         if (hash_found) then
  8860.             @<Determine the width of this accented or foreign character@>;
  8861.         end;
  8862.     while ((ex_buf_ptr < ex_buf_length) and
  8863.                 (lex_class[ex_buf[ex_buf_ptr]] = white_space)) do
  8864.     incr(ex_buf_ptr);                   {this skips following |white_space|}
  8865.     while ((ex_buf_ptr < ex_buf_length) and (brace_level > 0) and
  8866.                                         (ex_buf[ex_buf_ptr] <> backslash)) do
  8867.         begin                   {this scans to the next control sequence}
  8868.         if (ex_buf[ex_buf_ptr] = right_brace) then
  8869.             decr(brace_level)
  8870.         else if (ex_buf[ex_buf_ptr] = left_brace) then
  8871.             incr(brace_level)
  8872.         else
  8873.             string_width := string_width + char_width[ex_buf[ex_buf_ptr]];
  8874.         incr(ex_buf_ptr);
  8875.         end;
  8876.     end;
  8877. decr(ex_buf_ptr);                       {unskip the |right_brace|}
  8878. Five of the 13 possibilities resort to special information not present
  8879. in the |char_width| array; the other eight simply use |char_width|'s
  8880. information for the first letter of the control sequence.
  8881. @<Determine the width of this accented or foreign character@>=
  8882. begin
  8883. case (ilk_info[control_seq_loc]) of
  8884.     n_ss : string_width := string_width + ss_width;
  8885.     n_ae : string_width := string_width + ae_width;
  8886.     n_oe : string_width := string_width + oe_width;
  8887.     n_ae_upper : string_width := string_width + upper_ae_width;
  8888.     n_oe_upper : string_width := string_width + upper_oe_width;
  8889.     othercases
  8890.         string_width := string_width + char_width[ex_buf[ex_buf_xptr]]
  8891. endcases;
  8892. The |built_in| function {\.{write\$}} pops the top (string) literal
  8893. and writes it onto the output buffer |out_buf| (which will result in
  8894. stuff being written onto the \.{.bbl} file if the buffer fills up).  If
  8895. the literal isn't a string, it complains but does nothing else.
  8896. @<|execute_fn|({\.{write\$}})@>=
  8897. procedure x_write;
  8898. begin
  8899. pop_lit_stk (pop_lit1,pop_typ1);
  8900. if (pop_typ1 <> stk_str) then
  8901.     print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str)
  8902.     add_out_pool (pop_lit1);
  8903. @* Cleaning up.
  8904. @^clich\'e-\`a-trois@>
  8905. @^fat lady@>
  8906. @^turn out lights@>
  8907. @^Yogi@>
  8908. This section does any last-minute printing and ends the program.
  8909. @<Clean up and leave@>=
  8910. begin
  8911. if ((read_performed) and (not reading_completed)) then
  8912.     begin
  8913.     print ('Aborted at line ',bib_line_num:0,' of file ');
  8914.     print_bib_name;
  8915.     end;
  8916. trace_and_stat_printing;
  8917. @<Print the job |history|@>;
  8918. a_close (log_file);
  8919. {turn out the lights, the fat lady has sung; it's over, Yogi}
  8920. Here we print |trace| and/or |stat| information, if desired.
  8921. @<Procedures and functions for all file I/O, error messages, and such@>=
  8922. procedure trace_and_stat_printing;
  8923. begin
  8924.   trace
  8925.   @<Print all \.{.bib}- and \.{.bst}-file information@>;
  8926.   @<Print all |cite_list| and entry information@>;
  8927.   @<Print the |wiz_defined| functions@>;
  8928.   @<Print the string pool@>;
  8929.   ecart@/
  8930.   stat
  8931.   @<Print usage statistics@>;
  8932.   tats@/
  8933. This prints information obtained from the \.{.aux} file about the
  8934. other files.
  8935. @<Print all \.{.bib}- and \.{.bst}-file information@>=
  8936. begin
  8937. if (num_bib_files = 1) then
  8938.     trace_pr_ln ('The 1 database file is')
  8939.   else
  8940.     trace_pr_ln ('The ',num_bib_files:0,' database files are');
  8941. if (num_bib_files = 0) then
  8942.     trace_pr_ln ('   undefined')
  8943.   else
  8944.     begin
  8945.     bib_ptr := 0;
  8946.     while (bib_ptr < num_bib_files) do
  8947.         begin
  8948.         trace_pr ('   ');
  8949.         trace_pr_pool_str (cur_bib_str);
  8950.         trace_pr_pool_str (s_bib_extension);
  8951.         trace_pr_newline;
  8952.         incr(bib_ptr);
  8953.         end;
  8954.     end;
  8955. trace_pr ('The style file is ');
  8956. if (bst_str = 0) then
  8957.     trace_pr_ln ('undefined')
  8958.   else
  8959.     begin
  8960.     trace_pr_pool_str (bst_str);
  8961.     trace_pr_pool_str (s_bst_extension);
  8962.     trace_pr_newline;
  8963.     end;
  8964. In entry-sorted order, this prints an entry's |cite_list| string and,
  8965. indirectly, its entry type and entry variables.
  8966. @<Print all |cite_list| and entry information@>=
  8967. begin
  8968. if (all_entries) then
  8969.     trace_pr ('all_marker=',all_marker:0,', ');
  8970. if (read_performed) then
  8971.     trace_pr_ln ('old_num_cites=',old_num_cites:0)
  8972.   else
  8973.     trace_pr_newline;
  8974. trace_pr ('The ',num_cites:0);
  8975. if (num_cites = 1) then
  8976.     trace_pr_ln (' entry:')
  8977.   else
  8978.     trace_pr_ln (' entries:');
  8979. if (num_cites = 0) then
  8980.     trace_pr_ln ('   undefined')
  8981.   else
  8982.     begin
  8983.     sort_cite_ptr := 0;
  8984.     while (sort_cite_ptr < num_cites) do
  8985.         begin
  8986.         if (not read_completed) then    {we didn't finish the \.{read} command}
  8987.             cite_ptr := sort_cite_ptr
  8988.           else
  8989.             cite_ptr := sorted_cites[sort_cite_ptr];
  8990.         trace_pr_pool_str (cur_cite_str);
  8991.         if (read_performed) then
  8992.             @<Print entry information@>
  8993.           else
  8994.             trace_pr_newline;
  8995.         incr(sort_cite_ptr);
  8996.         end;
  8997.     end;
  8998. This prints information gathered while reading the \.{.bst} and
  8999. \.{.bib} files.
  9000. @<Print entry information@>=
  9001. begin
  9002. trace_pr (', entry-type ');
  9003. if (type_list[cite_ptr] = undefined) then
  9004.     undefined : trace_pr ('unknown')
  9005. else if (type_list[cite_ptr] = empty) then
  9006.     trace_pr ('--- no type found')
  9007.     trace_pr_pool_str (hash_text[type_list[cite_ptr]]);
  9008. trace_pr_ln (', has entry strings');
  9009. @<Print entry strings@>;
  9010. trace_pr ('  has entry integers');
  9011. @<Print entry integers@>;
  9012. trace_pr_ln ('  and has fields');
  9013. @<Print fields@>;
  9014. This prints, for the current entry, the strings declared by the
  9015. \.{entry} command.
  9016. @<Print entry strings@>=
  9017. begin
  9018. if (num_ent_strs = 0) then
  9019.     trace_pr_ln ('    undefined')
  9020. else if (not read_completed) then
  9021.     trace_pr_ln ('    uninitialized')
  9022.     begin
  9023.     str_ent_ptr := cite_ptr * num_ent_strs;
  9024.     while (str_ent_ptr < (cite_ptr+1)*num_ent_strs) do
  9025.         begin
  9026.         ent_chr_ptr := 0;
  9027.         trace_pr ('    "');
  9028.         while (entry_strs[str_ent_ptr][ent_chr_ptr] <> end_of_string) do
  9029.             begin
  9030.             trace_pr (xchr[entry_strs[str_ent_ptr][ent_chr_ptr]]);
  9031.             incr(ent_chr_ptr);
  9032.             end;
  9033.         trace_pr_ln ('"');
  9034.         incr(str_ent_ptr);
  9035.         end;
  9036.     end;
  9037. This prints, for the current entry, the integers declared by the
  9038. \.{entry} command.
  9039. @<Print entry integers@>=
  9040. begin
  9041. if (num_ent_ints = 0) then
  9042.     trace_pr (' undefined')
  9043. else if (not read_completed) then
  9044.     trace_pr (' uninitialized')
  9045.     begin
  9046.     int_ent_ptr := cite_ptr*num_ent_ints;
  9047.     while (int_ent_ptr < (cite_ptr+1)*num_ent_ints) do
  9048.         begin
  9049.         trace_pr (' ',entry_ints[int_ent_ptr]:0);
  9050.         incr(int_ent_ptr);
  9051.         end;
  9052.     end;
  9053. trace_pr_newline;
  9054. This prints the fields stored for the current entry.
  9055. @<Print fields@>=
  9056. begin
  9057. if (not read_performed) then
  9058.     trace_pr_ln ('    uninitialized')
  9059.   else
  9060.     begin
  9061.     field_ptr := cite_ptr * num_fields;
  9062.     field_end_ptr := field_ptr + num_fields;
  9063.     no_fields := true;
  9064.     while (field_ptr < field_end_ptr) do
  9065.         begin
  9066.         if (field_info[field_ptr] <> missing) then
  9067.             begin
  9068.             trace_pr ('    "');
  9069.             trace_pr_pool_str (field_info[field_ptr]);
  9070.             trace_pr_ln ('"');
  9071.             no_fields := false;
  9072.             end;
  9073.         incr(field_ptr);
  9074.         end;
  9075.     if (no_fields) then
  9076.         trace_pr_ln ('    missing');
  9077.     end;
  9078. This gives all the |wiz_defined| functions that appeared in the
  9079. \.{.bst} file.
  9080. @<Print the |wiz_defined| functions@>=
  9081. begin
  9082. trace_pr_ln ('The wiz-defined functions are');
  9083. if (wiz_def_ptr = 0) then
  9084.     trace_pr_ln ('   nonexistent')
  9085.   else
  9086.     begin
  9087.     wiz_fn_ptr := 0;
  9088.     while (wiz_fn_ptr < wiz_def_ptr) do
  9089.         begin
  9090.         if (wiz_functions[wiz_fn_ptr] = end_of_def) then
  9091.             trace_pr_ln (wiz_fn_ptr:0,'--end-of-def--')
  9092.         else if (wiz_functions[wiz_fn_ptr] = quote_next_fn) then
  9093.             trace_pr (wiz_fn_ptr:0,'  quote_next_function    ')
  9094.         else
  9095.             begin
  9096.             trace_pr (wiz_fn_ptr:0,'  `');
  9097.             trace_pr_pool_str (hash_text[wiz_functions[wiz_fn_ptr]]);
  9098.             trace_pr_ln ('''');
  9099.             end;
  9100.         incr(wiz_fn_ptr);
  9101.         end;
  9102.    end;
  9103. This includes all the `static' strings (that is, those that are also
  9104. in the hash table), but none of the dynamic strings (that is, those
  9105. put on the stack while executing \.{.bst} commands).
  9106. @<Print the string pool@>=
  9107. begin
  9108. trace_pr_ln ('The string pool is');
  9109. str_num := 1;
  9110. while (str_num < str_ptr) do
  9111.     begin
  9112.     trace_pr (str_num:4, str_start[str_num]:6,' "');
  9113.     trace_pr_pool_str (str_num);
  9114.     trace_pr_ln ('"');
  9115.     incr(str_num);
  9116.     end;
  9117. @^statistics@>
  9118. These statistics can help determine how large some of the constants
  9119. should be and can tell how useful certain |built_in| functions are.
  9120. They are written to the same files as tracing information.
  9121. @d stat_pr == trace_pr
  9122. @d stat_pr_ln == trace_pr_ln
  9123. @d stat_pr_pool_str == trace_pr_pool_str
  9124. @<Print usage statistics@>=
  9125. begin
  9126. stat_pr ('You''ve used ',num_cites:0);
  9127. if (num_cites = 1) then
  9128.     stat_pr_ln (' entry,')
  9129.   else
  9130.     stat_pr_ln (' entries,');
  9131. stat_pr_ln ('            ',wiz_def_ptr:0,' wiz_defined-function locations,');
  9132. stat_pr_ln ('            ',str_ptr:0,' strings with ',str_start[str_ptr]:0,
  9133.                                                         ' characters,');
  9134. blt_in_ptr := 0;
  9135. total_ex_count := 0;
  9136. while (blt_in_ptr < num_blt_in_fns) do
  9137.     begin
  9138.     total_ex_count := total_ex_count + execution_count[blt_in_ptr];
  9139.     incr(blt_in_ptr);
  9140.     end;
  9141. stat_pr_ln ('and the built_in function-call counts, ', total_ex_count:0,
  9142.                                                         ' in all, are:');
  9143. blt_in_ptr := 0;
  9144. while (blt_in_ptr < num_blt_in_fns) do
  9145.     begin
  9146.     stat_pr_pool_str (hash_text[blt_in_loc[blt_in_ptr]]);
  9147.     stat_pr_ln (' -- ',execution_count[blt_in_ptr]:0);
  9148.     incr(blt_in_ptr);
  9149.     end;
  9150. @^bunk, history@>
  9151. @^system dependencies@>
  9152. @:this can't happen}{\quad History is bunk@>
  9153. Some implementations may wish to pass the |history| value to the
  9154. operating system so that it can be used to govern whether or not other
  9155. programs are started. Here we simply report the history to the user.
  9156. @<Print the job |history|@>=
  9157. case (history) of
  9158.     spotless : do_nothing;
  9159.     warning_message : begin
  9160.                       if (err_count = 1) then
  9161.                           print_ln ('(There was 1 warning)')
  9162.                         else
  9163.                           print_ln ('(There were ',err_count:0,' warnings)');
  9164.                       end;
  9165.     error_message : begin
  9166.                     if (err_count = 1) then
  9167.                         print_ln ('(There was 1 error message)')
  9168.                       else
  9169.                         print_ln ('(There were ',err_count:0,
  9170.                                                         ' error messages)');
  9171.                     end;
  9172.     fatal_message : print_ln ('(That was a fatal error)');
  9173.     othercases begin
  9174.                print ('History is bunk');
  9175.                print_confusion;
  9176.                end
  9177. endcases
  9178. @* System-dependent changes.
  9179. @^system dependencies@>
  9180. This section should be replaced, if necessary, by changes to the program
  9181. that are necessary to make \BibTeX\ work at a particular installation.
  9182. It is usually best to design your change file so that all changes to
  9183. previous sections preserve the section numbering; then everybody's version
  9184. will be consistent with the printed program. More extensive changes,
  9185. which introduce new sections, can be inserted here; then only the index
  9186. itself will get a new section number.
  9187. @* Index.
  9188. @.this can't happen@>
  9189. Here is where you can find all uses of each identifier in the program,
  9190. with underlined entries pointing to where the identifier was defined.
  9191. If the identifier is only one letter long, however, you get to see only
  9192. the underlined entries. All references are to section numbers instead of
  9193. page numbers.
  9194. This index also lists a few error messages and other aspects of the
  9195. program that you might want to look up some day. For example, the
  9196. entry for ``system dependencies'' lists all sections that should
  9197. receive special attention from people who are installing \TeX\ in a
  9198. new operating environment. A list of various things that can't happen
  9199. appears under ``this can't happen''$\!$.
  9200.